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
Field | Description |
---|---|
package (common) | The name of the Go package |
aliases | Maps Apexlang aliases to specific Go types |
Callbacks
Name | Description |
---|---|
StructTags | Write additional struct tags to structs |
UnionStructTags | Write additional struct tags to union structs |
Overridable Nested Visitors
Class | Description | Override function(s) |
---|---|---|
ImportsVisitor | Writes the imports required by the .go file. | importsVisitor |
InterfaceVisitor | Writes an Go interface for all @service and @dependency Apexlang interfaces. | serviceVisitor , dependencyVisitor |
StructVisitor | Writes a simple Go struct for Apexlang types. | structVisitor |
EnumVisitor | Writes an enum using const and various func s. | enumVisitor |
UnionVisitor | Writes an Apexlang union as a Go struct with optional fields for each declared type. | unionVisitor |
AliasVisitor | Writes 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
Field | Description |
---|---|
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
Field | Description |
---|---|
package (common) | The name of the Go package |
names | The names of specific interfaces to generate |
types | The 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
Field | Description |
---|---|
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
Field | Description |
---|---|
package (common) | The name of the Go package |
protoPackage | The package name of the gRPC generated code |