Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 97 additions & 3 deletions fern/products/sdks/overview/go/custom-code.mdx
Original file line number Diff line number Diff line change
@@ -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.
<Markdown src="/products/sdks/snippets/custom-code-intro.mdx"/>

<Warning>This page is a WIP, please refer to our previous [documentation](https://buildwithfern.com/learn/sdks/capabilities/custom-code).</Warning>
## Adding custom logic

<Markdown src="/products/sdks/snippets/custom-logic-intro.mdx"/>

<Steps>

### Create a new file and add your custom logic

```go title="helper.go"
func MyHelper() {
fmt.Println("Hello World!")
}
```

### Add your file to `.fernignore`

<Tip>A `.fernignore` file is automatically created in your SDK repository when you use GitHub publishing.</Tip>


```yaml {3} title=".fernignore"
# Specify files that shouldn't be modified by Fern

helper.go
```

### Consume the helper

<Markdown src="/products/sdks/snippets/consume-method.mdx"/>

```go
import "github.com/package/example"

example.MyHelper();
```
</Steps>

## Adding custom SDK methods

<Markdown src="/products/sdks/snippets/custom-sdk-methods-intro.mdx"/>

<Steps>
### 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():
```

<Markdown src="/products/sdks/snippets/consume-method.mdx"/>

```go
client.MyHelper()
```
</Steps>