Example code

Hertz provides a series of code examples designed to help users get start with Hertz and be familiar with its features.

Hertz provides a series of code examples designed to help users get start with Hertz and be familiar with its features. Refer to hertz-examples for more information.



  • hertz_gorm :Example of using gorm in hertz server


  • hertz_gorm_gen :Example of using gorm/gen & proto IDL in hertz server


  • hertz_jwt :Example of using jwt in hertz server


  • hertz_session :Example of using distributed session and csrf in hertz server


  • tiktok_demo :A simple hertz server that provides user, video, interactive, and social backend services for tiktok app



Run hertz

  • hello :Example of launching a hertz “hello world” application


  • config :Example of configuring hertz server


  • HTTP1 : Example of hertz using HTTP1 protocol
  • TLS : Example of hertz using TLS protocol
  • HTTP2 : Example of hertz using HTTP2 protocol
  • HTTP3 : Example of hertz using HTTP3 protocol
  • Websocket : Example of hertz using Websocket protocol
  • SSE : Example of hertz using SSE protocol


  • Route :Examples of registering routes, using route groups, and parameter routes


  • basic_auth :Example of using basic auth middleware
  • CORS :Example of using the CORS middleware
  • custom :Example of custom middleware
  • pprof :Example of using pprof middleware
  • requestid :Example of using RequestID middleware
  • gzip :Example of using gzip middleware in hertz server
  • csrf :Example of using csrf middleware in hertz server
  • loadbalance :Example of using loadbalance middleware in hertz server
  • Recovery :Example of using Recovery middleware
  • jwt :Example of using jwt middleware
  • i18n :Example of using i18n middleware
  • session :Example of using session middleware
  • KeyAuth :Example of using KeyAuth middleware
  • Swagger :Example of using Swagger middleware
  • access log :Example of using access log middleware
  • Secure :Example of using Secure middleware
  • Sentry :Example of using Sentry middleware
  • Casbin :Example of using Casbin middleware
  • ETag :Example of using ETag middleware
  • Cache :Example of using Cache middleware
  • Paseto :Example of using Paseto middleware

Parameter binding and validation

  • binding :Example of parameter binding and validation

Get Parameters

  • parameters :Example of getting query, form, cookie, etc. parameters


  • file :Examples of file upload, file download, and static file services


  • render :Example of render body as json, html, protobuf, etc


  • redirect :Example of a redirect to an internal/external URI

Streaming read/write

  • streaming :Example of streaming read/write using hertz server

Graceful shutdown

Unit test

  • unit_test :Example of writing unit tests using the interface provided by hertz without network transmission


  • tracer :Example of hertz using Jaeger for link tracing


  • monitoring :hertz Example of metrics monitoring with Prometheus

Multiple service


  • adaptor :Example of using adaptor to integrate hertz with package built for http.Handler interface , including a demonstration on using jade as template engine.


  • sentinel :Example of using sentinel-golang in hertz

Reverse proxy

  • reverseproxy :Example of using reverse proxy in hertz server


  • standard :Example of using Hertz’s default implementation for logging
  • custom :Example of log extension
  • zap :Example of docking with zap and lumberjack in hertz server
  • logrus :Example of docking with logrus and lumberjack in hertz server
  • zerolog :Example of docking with zerolog and lumberjack in hertz server
  • slog :Example of docking with slog and lumberjack in hertz server


  • opentelemetry :Example of using obs opentelemetry for docking with opentelemetry

HTTP Trailer

  • trailer :Example of using HTTP Trailer


Send request

  • send_request :Example of sending an http request using the hertz client

Client config


  • tls :Example of hertz client sending a tls request

Add parameters

  • add_parameters :Example of adding request parameters using the hertz client

Upload file

  • upload_file :Example of uploading a file using the hertz client


  • middleware :Example of using the hertz client middleware

Streaming read

  • streaming_read :Example of a streaming read response using the hertz client

Forward proxy

  • forward_proxy :Example of configuring a forward proxy using the hertz client

HTTP Trailer

  • trailer :Example of using HTTP Trailer


Generate server code based on Thrift

  • thrift :Example of using hz with thrift to generate server code

Generate server code based on Protobuf

  • protobuf :Example of using hz with protobuf to generate server code

Generate client code

  • hz_client :Example of using hz to generate client code

Custom templates

  • template :Example of using hz custom templates to generate server code

Three-party plugins

  • plugin :Example of using hz to access third-party plugins

Last modified June 26, 2024 : doc: add construct use case (#1098) (83b7cf9)