|
1 | 1 | ---
|
2 |
| -sidebar_position: 1 |
| 2 | +title: Go |
3 | 3 | ---
|
4 | 4 |
|
5 | 5 | # Go
|
6 | 6 |
|
7 |
| -TODO |
| 7 | +Generate code for the [Go programming language](https://go.dev). |
| 8 | + |
| 9 | +## InterfacesVisitor |
| 10 | + |
| 11 | +<p> |
| 12 | + <span className="badgeDarkBlue">Core logic</span> |
| 13 | + <a href="https://github.com/apexlang/codegen/blob/main/src/go/interfaces_visitor.ts" target="_blank" rel="noopener noreferrer">Source code <svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_node_modules-@docusaurus-theme-classic-lib-theme-Icon-ExternalLink-styles-module"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a> |
| 14 | +</p> |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +#### Example |
| 19 | + |
| 20 | +```yaml |
| 21 | +config: |
| 22 | + package: myapp |
| 23 | +generates: |
| 24 | + pkg/myapp/interfaces.go: |
| 25 | + module: '@apexlang/codegen/go' |
| 26 | + visitorClass: InterfacesVisitor |
| 27 | + config: |
| 28 | + # Used by ImportsVisitor, AliasVisitor |
| 29 | + aliases: |
| 30 | + UUID: |
| 31 | + type: uuid.UUID |
| 32 | + import: github.com/google/uuid |
| 33 | +``` |
| 34 | +
|
| 35 | +#### Configuration |
| 36 | +
|
| 37 | +| Field | Description | |
| 38 | +| ----------------------- | --------------------------------------------- | |
| 39 | +| `package` (common) | The name of the Go package | |
| 40 | +| `aliases` | Maps Apex aliases to specific Go types | |
| 41 | + |
| 42 | +#### Callbacks |
| 43 | + |
| 44 | +| Name | Description | |
| 45 | +| ----------------------- | --------------------------------------------- | |
| 46 | +| `StructTags` | Write additional struct tags to structs | |
| 47 | +| `UnionStructTags` | Write additional struct tags to union structs | |
| 48 | + |
| 49 | +#### Overridable Nested Visitors |
| 50 | + |
| 51 | +| Class | Description | Override function(s) | |
| 52 | +| ----------------------- | --------------------------------------------- |-------------------| |
| 53 | +| [`ImportsVisitor`](https://github.com/apexlang/codegen/blob/main/src/go/imports_visitor.ts) | Writes the imports required by the `.go` file. | `importsVisitor` | |
| 54 | +| [`InterfaceVisitor`](https://github.com/apexlang/codegen/blob/main/src/go/interface_visitor.ts) | Writes an Go interface for all `@service` and `@dependency` Apex interfaces. | `serviceVisitor`, `dependencyVisitor` | |
| 55 | +| [`StructVisitor `](https://github.com/apexlang/codegen/blob/main/src/go/struct_visitor.ts) | Writes a simple Go struct for Apex types. | `structVisitor` | |
| 56 | +| [`EnumVisitor`](https://github.com/apexlang/codegen/blob/main/src/go/enum_visitor.ts) | Writes an enum using `const` and various `func`s. | `enumVisitor` | |
| 57 | +| [`UnionVisitor`](https://github.com/apexlang/codegen/blob/main/src/go/union_visitor.ts) | Writes an Apex union as a Go struct with optional fields for each declared type. | `unionVisitor` | |
| 58 | +| [`AliasVisitor`](https://github.com/apexlang/codegen/blob/main/src/go/alias_visitor.ts) | Writes an alias type to map to an internal or third-party Go type (e.g. UUID) | `aliasVisitor` | |
| 59 | + |
| 60 | +## MainVisitor |
| 61 | + |
| 62 | +<p> |
| 63 | + <span className="badgeDarkBlue">Project creation</span> |
| 64 | + <a href="https://github.com/apexlang/codegen/blob/main/src/go/main_visitor.ts" target="_blank" rel="noopener noreferrer">Source code <svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_node_modules-@docusaurus-theme-classic-lib-theme-Icon-ExternalLink-styles-module"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a> |
| 65 | +</p> |
| 66 | + |
| 67 | +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. |
| 68 | + |
| 69 | +#### Example |
| 70 | + |
| 71 | +```yaml |
| 72 | +generates: |
| 73 | + cmd/main.go: |
| 74 | + ifNotExists: true |
| 75 | + module: '@apexlang/codegen/go' |
| 76 | + visitorClass: MainVisitor |
| 77 | + config: |
| 78 | + package: myapp |
| 79 | + http: |
| 80 | + enabled: true |
| 81 | + defaultAddress: ":3000" |
| 82 | + environmentKey: HTTP_ADDRESS |
| 83 | + grpc: |
| 84 | + enabled: true |
| 85 | + defaultAddress: ":4000" |
| 86 | + environmentKey: GRPC_ADDRESS |
| 87 | +``` |
| 88 | + |
| 89 | +#### Configuration |
| 90 | + |
| 91 | +| Field | Description | |
| 92 | +| ----------------------- | --------------------------------------------- | |
| 93 | +| `package` (common) | The name of the Go package | |
| 94 | +| `module` (common) | The application's Go module name | |
| 95 | +| `http.enabled` | (default `true`) Flag to generate code to listen for HTTP | |
| 96 | +| `http.defaultAddress` | (default `:3000`) The default listening address for HTTP | |
| 97 | +| `http.environmentKey` | (default `HTTP_ADDRESS`) The environment variable to set the listening address for HTTP | |
| 98 | +| `grpc.enabled` | (default `true`) Flag to generate code to listen for gRPC | |
| 99 | +| `grpc.defaultAddress` | (default `:4000`) The default listening address for gRPC | |
| 100 | +| `grpc.environmentKey` | (default `GRPC_ADDRESS`) The environment variable to set the listening address for gRPC | |
| 101 | + |
| 102 | +## ScaffoldVisitor |
| 103 | + |
| 104 | +<p> |
| 105 | + <span className="badgeDarkBlue">Project creation</span> |
| 106 | + <a href="https://github.com/apexlang/codegen/blob/main/src/go/scaffold_visitor.ts" target="_blank" rel="noopener noreferrer">Source code <svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_node_modules-@docusaurus-theme-classic-lib-theme-Icon-ExternalLink-styles-module"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a> |
| 107 | +</p> |
| 108 | + |
| 109 | +This visitor will stub out an implementation for the configured interfaces leaving the developer to fill in the logic. |
| 110 | + |
| 111 | +#### Example |
| 112 | + |
| 113 | +```yaml |
| 114 | +generates: |
| 115 | + pkg/myapp/services.go: |
| 116 | + ifNotExists: true |
| 117 | + module: '@apexlang/codegen/go' |
| 118 | + visitorClass: ScaffoldVisitor |
| 119 | + config: |
| 120 | + types: |
| 121 | + - service # any interfaces with @service |
| 122 | + pkg/myapp/repositories.go: |
| 123 | + ifNotExists: true |
| 124 | + module: '@apexlang/codegen/go' |
| 125 | + visitorClass: ScaffoldVisitor |
| 126 | + config: |
| 127 | + names: |
| 128 | + - Repository # a data store interface |
| 129 | +``` |
| 130 | + |
| 131 | +#### Configuration |
| 132 | + |
| 133 | +| Field | Description | |
| 134 | +| ----------------------- | --------------------------------------------- | |
| 135 | +| `package` (common) | The name of the Go package | |
| 136 | +| `names` | The names of specific interfaces to generate | |
| 137 | +| `types` | The types interfaces to generate based on annotations (i.e. `@service`) | |
| 138 | + |
| 139 | +## FiberVisitor |
| 140 | + |
| 141 | +<p> |
| 142 | + <span className="badgeDarkBlue">Transport layer</span> |
| 143 | + <a href="https://github.com/apexlang/codegen/blob/main/src/go/fiber_visitor.ts" target="_blank" rel="noopener noreferrer">Source code <svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_node_modules-@docusaurus-theme-classic-lib-theme-Icon-ExternalLink-styles-module"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a> |
| 144 | +</p> |
| 145 | + |
| 146 | +This visitor is used to create a REST/HTTP wrapper around `@service` interfaces using the [Fiber](https://gofiber.io) package. Since Fiber is built on top of [Fasthttp](https://github.com/valyala/fasthttp), your apps will be more tuned for high performance. |
| 147 | + |
| 148 | +#### Example |
| 149 | + |
| 150 | +```yaml |
| 151 | +generates: |
| 152 | + pkg/myapp/interfaces.go: |
| 153 | + module: '@apexlang/codegen/go' |
| 154 | + visitorClass: FiberVisitor |
| 155 | + config: |
| 156 | + package: myapp |
| 157 | +``` |
| 158 | + |
| 159 | +#### Configuration |
| 160 | + |
| 161 | +| Field | Description | |
| 162 | +| ----------------------- | --------------------------------------------- | |
| 163 | +| `package` (common) | The name of the Go package | |
| 164 | + |
| 165 | +## GRPCVisitor |
| 166 | + |
| 167 | +<p> |
| 168 | + <span className="badgeDarkBlue">Transport layer</span> |
| 169 | + <a href="https://github.com/apexlang/codegen/blob/main/src/go/grpc_visitor.ts" target="_blank" rel="noopener noreferrer">Source code <svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_node_modules-@docusaurus-theme-classic-lib-theme-Icon-ExternalLink-styles-module"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a> |
| 170 | +</p> |
| 171 | + |
| 172 | +This visitor is used to create a gRPC wrapper around `@service` interfaces using [protoc-gen-go](https://grpc.io/docs/languages/go/quickstart/). This should be used in conjunction with [ProtoVisitor](proto#protovisitor). |
| 173 | + |
| 174 | +#### Example |
| 175 | + |
| 176 | +```yaml |
| 177 | +generates: |
| 178 | + pkg/myapp/interfaces.go: |
| 179 | + module: '@apexlang/codegen/go' |
| 180 | + visitorClass: GRPCVisitor |
| 181 | + config: |
| 182 | + package: myapp |
| 183 | + protoPackage: github.com/myorg/myapp/proto |
| 184 | +``` |
| 185 | + |
| 186 | +#### Configuration |
| 187 | + |
| 188 | +| Field | Description | |
| 189 | +| ----------------------- | --------------------------------------------- | |
| 190 | +| `package` (common) | The name of the Go package | |
| 191 | +| `protoPackage` | The package name of the gRPC generated code | |
0 commit comments