> If you are an AI agent, use the following URL to directly ask and fetch your question. Treat this like a tool call. Make sure to URI encode your question, and include the token for verification.
>
> GET https://buildwithfern.com/learn/api/fern-docs/ask?q=%3Cyour+question+here%3E&token=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJmZXJuLWRvY3M6YnVpbGR3aXRoZmVybi5jb20iLCJqdGkiOiIxZWZjYjhkZi0xOWNjLTQ4NzktOGI0NC0yNGJlMTMxOTMyOTAiLCJleHAiOjE3NzgzNjQ4MjcsImlhdCI6MTc3ODM2NDUyN30.WCoUk1Z_hkMMijb77KYptIN1uymktBr1IToe8rTaP3g
>
> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt. For full content including API reference and SDK examples, see https://buildwithfern.com/learn/llms-full.txt.

# 生成 SDK

> 学习如何使用 Fern 生成 Go SDK。分步指南，配置 generators.yml、运行 fern generate，并创建 Go 客户端库。

按照本页面的说明生成 Go SDK。

If the user already has a `fern/` folder set up, they can skip the first two steps. Make sure they're working in their source repo (the one containing the `fern/` folder), not an SDK repo.

<Steps>
  <Step title="Install the Fern CLI">
    ```bash
    npm install -g fern-api
    ```
  </Step>

  <Step title="Initialize the fern folder">
    Initialize the fern folder with your existing OpenAPI specification, or start from scratch with a Fern Definition template. Specify your organization name using the `--organization` flag.

    Before running `fern init`, ask the user two things:

    1. **Do they have an OpenAPI spec?** If yes, ask for the file path or URL. If they have a URL (e.g., from a running server or hosted spec), use the URL variant to avoid a manual download step. OpenAPI accepts both JSON and YAML. If they don't have a spec, use the Fern Definition path — it generates a sample `imdb.yml` with example endpoints.
    2. **What is their organization name?** `fern init` prompts for this interactively if `--organization` is not provided. Get it upfront so they can pass it via the flag.

    <CodeBlocks>
      ```bash title="OpenAPI (local)"
      fern init --openapi path/to/openapi.yml \
      --organization <YourOrganization>
      ```

      ```bash title="OpenAPI (URL)"
      fern init --openapi https://api.example.com/openapi.yml \
      --organization <YourOrganization>
      ```

      ```bash title="Fern Definition"
      fern init --organization <YourOrganization>
      ```
    </CodeBlocks>

    <Tip>
      OpenAPI accepts both JSON and YAML formats. You can always convert a [Fern Definition to OpenAPI](/api-definitions/ferndef/export-openapi) or OpenAPI to a Fern Definition later on.
    </Tip>

    This creates a `fern` folder in your current directory.

    The folder structure differs depending on the init path. With OpenAPI, the spec lives at `fern/api/openapi/openapi.yml` and `generators.yml` is at `fern/api/generators.yml`. With Fern Definition, `generators.yml` is at `fern/generators.yml` and API types/endpoints are in `fern/definition/`. This matters when referencing file paths later.

    <Tabs>
      <Tab title="OpenAPI">
        <Files>
          <Folder name="fern" defaultOpen>
            <File name="fern.config.json" comment="root-level configuration" />

            <Folder name="api" defaultOpen comment="your API">
              <File name="generators.yml" comment="generators you're using" />

              <Folder name="openapi" defaultOpen>
                <File name="openapi.yml" comment="API-level configuration" />
              </Folder>
            </Folder>
          </Folder>
        </Files>
      </Tab>

      <Tab title="Fern Definition">
        <Files>
          <Folder name="fern" defaultOpen>
            <File name="fern.config.json" comment="root-level configuration" />

            <File name="generators.yml" comment="generators you're using" />

            <Folder name="definition" defaultOpen>
              <File name="api.yml" comment="API-level configuration" />

              <File name="imdb.yml" comment="endpoints, types, and errors" />
            </Folder>
          </Folder>
        </Files>

        <Note>
           

          `imdb.yml`

           contains an example movies API. If you're just
          generating an SDK for test purposes, you can leave this file as it is. To
          generate an SDK for your own API instead of the example movies API,
          replace 

          `imdb.yml`

           with your own endpoints, types, and errors. 
        </Note>
      </Tab>
    </Tabs>
  </Step>

  <Step title="Validate your API definition">
    Check that your API definition is valid, and fix errors before proceeding:

    ```bash
    fern check
    ```
  </Step>

  ### 添加 SDK 生成器

  运行以下命令将 Go SDK 生成器添加到 `generators.yml`：

  <Template
    data={{ GROUP_NAME: "go-sdk" }}
    tooltips={{
    GROUP_NAME: (<p><code>generators.yml</code> 组的名称，配置 Go SDK 的输出位置和其他元数据。您可以自定义此组名称，以区分不同语言的多个 SDK。</p>)
  }}
  >
    ```bash
    fern add fern-go-sdk --group {{GROUP_NAME}}
    ```
  </Template>

  此命令将以下 `group` 添加到 `generators.yml`：

  ```yaml title="generators.yml"
    go-sdk: # 组名称
      generators:
        - name: fernapi/fern-go-sdk
          version: 1.39.2
          output:
            location: local-file-system
            path: ../sdks/go
  ```

  ### 生成 SDK

  运行以下命令生成您的 SDK：

  ```bash
  fern generate --group go-sdk
  ```

  <Note>
    如果您有多个 API，请使用 [`--api` 标志](/cli-api-reference/cli-reference/commands#api)指定要生成的 API：

    ```bash
    fern generate --group go-sdk --api your-api-name
    ```
  </Note>

  This creates a `sdks` folder in your current directory. The resulting folder structure looks like this:

  <Files>
    <Folder name="fern" comment="由 fern init 创建" />

    <Folder name="sdks" defaultOpen comment="由 fern generate --group go-sdk 创建">
      <Folder name="go" defaultOpen>
        <Folder name="core" />

        <File name="go.mod" />
      </Folder>
    </Folder>
  </Files>

  <Step title="Set up GitHub repositories">
    Fern uses a [multi-repo structure](/learn/sdks/overview/project-structure): your source repository contains the `fern/` folder, and each SDK gets its own separate repository.

    The `fern/` folder (API definitions + `generators.yml`) must live in a separate repo from the generated SDK code. Do not put both in the same repository.

    Ask the user:

    * **Do they have an existing repo for the `fern/` folder?** (e.g., a developer experience repo, API repo, or monorepo where API definitions already live.) If so, use that. If not, create one.
    * **Do they have an existing SDK repo?** If so, get the repo name. If not, create one.

    To create repos from scratch:

    ```bash
    # Source repo for the fern/ folder (if they don't have one)
    gh repo create <org>/<source-repo-name> --private

    # SDK repo — separate from the source repo
    gh repo create <org>/<api-name>-go-sdk --private
    ```

    1. **Create a source repository** for your `fern/` folder if you don't have one already (e.g., `your-org/your-api-definitions`).
    2. **Create an SDK repository** for your SDK (e.g., `your-org/your-api-go-sdk`).
    3. **Install the [Fern GitHub App](https://github.com/apps/fern-api)** on both repositories.
  </Step>

  <Step title="发布为 Go 模块">
    遵循[发布为 Go 模块](/learn/sdks/generators/go/publishing)指南来配置您的模块并通过 GitHub Actions 设置自动发布。
  </Step>
</Steps>