Skip to main content

Go

Generate code for the Go programming language.

InterfacesVisitor

Core logicSource code

Generates interfaces annotated with @service and @dependency along with structs and enums. These interfaces act as the main contract between the application's core logic and external dependencies such as other APIs or datasources.

Example

config:
package: myapp
generates:
pkg/myapp/interfaces.go:
module: 'https://deno.land/x/apex_codegen@v0.1.9/go/mod.ts'
visitorClass: InterfacesVisitor
config:
# Used by ImportsVisitor, AliasVisitor
aliases:
UUID:
type: uuid.UUID
import: github.com/google/uuid

Configuration

FieldDescription
package (common)The name of the Go package
aliasesMaps Apexlang aliases to specific Go types

Callbacks

NameDescription
StructTagsWrite additional struct tags to structs
UnionStructTagsWrite additional struct tags to union structs

Overridable Nested Visitors

ClassDescriptionOverride function(s)
ImportsVisitorWrites the imports required by the .go file.importsVisitor
InterfaceVisitorWrites an Go interface for all @service and @dependency Apexlang interfaces.serviceVisitor, dependencyVisitor
StructVisitor Writes a simple Go struct for Apexlang types.structVisitor
EnumVisitorWrites an enum using const and various funcs.enumVisitor
UnionVisitorWrites an Apexlang union as a Go struct with optional fields for each declared type.unionVisitor
AliasVisitorWrites an alias type to map to an internal or third-party Go type (e.g. UUID)aliasVisitor

MainVisitor

Project creationSource code

This visitor creates the initial entry point (main.go) for your application and stubs out creation of services and dependencies and can server HTTP and/or gRPC.

Example

generates:
cmd/main.go:
ifNotExists: true
module: '@apexlang/codegen/go'
visitorClass: MainVisitor
config:
package: myapp
http:
enabled: true
defaultAddress: ":3000"
environmentKey: HTTP_ADDRESS
grpc:
enabled: true
defaultAddress: ":4000"
environmentKey: GRPC_ADDRESS

Configuration

FieldDescription
package (common)The name of the Go package
module (common)The application's Go module name
http.enabled(default true) Flag to generate code to listen for HTTP
http.defaultAddress(default :3000) The default listening address for HTTP
http.environmentKey(default HTTP_ADDRESS) The environment variable to set the listening address for HTTP
grpc.enabled(default true) Flag to generate code to listen for gRPC
grpc.defaultAddress(default :4000) The default listening address for gRPC
grpc.environmentKey(default GRPC_ADDRESS) The environment variable to set the listening address for gRPC

ScaffoldVisitor

Project creationSource code

This visitor will stub out an implementation for the configured interfaces leaving the developer to fill in the logic.

Example

generates:
pkg/myapp/services.go:
ifNotExists: true
module: '@apexlang/codegen/go'
visitorClass: ScaffoldVisitor
config:
types:
- service # any interfaces with @service
pkg/myapp/repositories.go:
ifNotExists: true
module: '@apexlang/codegen/go'
visitorClass: ScaffoldVisitor
config:
names:
- Repository # a data store interface

Configuration

FieldDescription
package (common)The name of the Go package
namesThe names of specific interfaces to generate
typesThe types interfaces to generate based on annotations (i.e. @service)

FiberVisitor

Transport layerSource code

This visitor is used to create a REST/HTTP wrapper around @service interfaces using the Fiber package. Since Fiber is built on top of Fasthttp, your apps will be more tuned for high performance.

Example

generates:
pkg/myapp/interfaces.go:
module: '@apexlang/codegen/go'
visitorClass: FiberVisitor
config:
package: myapp

Configuration

FieldDescription
package (common)The name of the Go package

GRPCVisitor

Transport layerSource code

This visitor is used to create a gRPC wrapper around @service interfaces using protoc-gen-go. This should be used in conjunction with ProtoVisitor.

Example

generates:
pkg/myapp/interfaces.go:
module: '@apexlang/codegen/go'
visitorClass: GRPCVisitor
config:
package: myapp
protoPackage: github.com/myorg/myapp/proto

Configuration

FieldDescription
package (common)The name of the Go package
protoPackageThe package name of the gRPC generated code