SDK
生成多种语言的客户端库
添加自定义代码
本页面介绍如何为你的 Go SDK 添加自定义逻辑和方法。
开始添加自定义代码:
1func MyHelper() {2 fmt.Println("Hello World!")3}
.fernignore
1# Specify files that shouldn't be modified by Fern23helper.go
Now your users can consume the helper function by importing it from the SDK.
1import "github.com/package/example"23example.MyHelper();
Fern also allows you to add custom methods to the SDK itself (e.g. client.my_method() ) by inheriting the Fern generated client and then extending it.
client.my_method()
generators.yml
将你的 Fern 生成的客户端命名为 BaseClient 之类的名称,以反映该客户端将被扩展。
BaseClient
1- name: fernapi/fern-go-sdk2 version: "..."3 config:4 clientName: BaseClient
首先,导入 Fern 生成的基础客户端并对其进行扩展。然后,添加你想要的任何方法。
1type MyClient struct {2 *Client // Embed the Fern generated client.3}45func NewMyClient(opts ...option.RequestOption) *MyClient {6 return &MyClient{7 Client: NewClient(opts...),8 }9}1011func (m *MyClient) MyHelper() {12 fmt.Println("Hello World!")13}
将 client/my_client.go 添加到 .fernignore。
client/my_client.go
1+ client/my_client.go
你的用户不需要构造生成的客户端,而是构造扩展的客户端。
1import exampleclient "github.com/package/example/client"23client := exampleclient.NewMyClient():
1client.MyHelper()