diff --git a/fern/products/sdks/overview/go/custom-code.mdx b/fern/products/sdks/overview/go/custom-code.mdx index b7780106c..ebf6accb6 100644 --- a/fern/products/sdks/overview/go/custom-code.mdx +++ b/fern/products/sdks/overview/go/custom-code.mdx @@ -1,8 +1,102 @@ --- title: Adding custom code -description: Augment your TypeScript SDK with custom utilities +description: Augment your Go SDK with custom utilities --- -Learn how to extend your Fern Go SDK with custom code and utilities. + -This page is a WIP, please refer to our previous [documentation](https://buildwithfern.com/learn/sdks/capabilities/custom-code). +## Adding custom logic + + + + + + ### Create a new file and add your custom logic + + ```go title="helper.go" + func MyHelper() { + fmt.Println("Hello World!") + } + ``` + + ### Add your file to `.fernignore` + + A `.fernignore` file is automatically created in your SDK repository when you use GitHub publishing. + + + ```yaml {3} title=".fernignore" + # Specify files that shouldn't be modified by Fern + + helper.go + ``` + + ### Consume the helper + + + + ```go + import "github.com/package/example" + + example.MyHelper(); + ``` + + + ## Adding custom SDK methods + + + + + ### Update `generators.yml` configuration + Name your Fern-generated client something like `BaseClient` to reflect that this client will be extended. + + ```yml {4} title="generators.yml" + - name: fernapi/fern-java-sdk + version: "..." + config: + client-class-name: BaseClient + ``` + + ### Import and extend the generated client + + First, import the Fern generated base client and extend it. Then, add whatever methods you want. + + ```go title="client/my_client.go" + type MyClient struct { + *Client // Embed the Fern generated client. + } + + func NewMyClient(opts ...option.RequestOption) *MyClient { + return &MyClient{ + Client: NewClient(opts...), + } + } + + func (m *MyClient) MyHelper() { + fmt.Println("Hello World!") + } + ``` + + ### Update `.fernignore` + + Add the `client/my_client.go.` to `.fernignore`. + + ```diff title=".fernignore" + + client/my_client.go + ``` + + ### Consume the method + + Instead of constructing the generated client, your users will want to construct the extended client. + + ```go title="main.go" + import exampleclient "github.com/package/example/client" + + client := exampleclient.NewMyClient(): + ``` + + + + ```go + client.MyHelper() + ``` + \ No newline at end of file