Github操作:golang找不到包

时间:2020-07-17 00:51:18

标签: go github-actions

我将示例github动作设置为我的存储库。片段在这里。

jobs:

  build:
    name: Build
    runs-on: ubuntu-latest
    steps:

    - name: Set up Go 1.x
      uses: actions/setup-go@v2
      with:
        go-version: ^1.13
      id: go

    - name: Check out code into the Go module directory
      uses: actions/checkout@v2

    - name: Get dependencies
      run: |
        go get -v -t -d ./...
        if [ -f Gopkg.toml ]; then
            curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
            dep ensure
        fi

但是作业在Get dependencies处失败。错误在这里。

package github.com/<organization-account>/<repo-name>/api/domain/repo: cannot find package "github.com/<organization-account>/<repo-name>/api/domain/repo" in any of:
    /opt/hostedtoolcache/go/1.14.4/x64/src/github.com/<organization-account>/<repo-name>/api/domain/repo (from $GOROOT)
    /home/runner/go/src/github.com/<organization-account>/<repo-name>/api/domain/repo (from $GOPATH)

当然。我的代码在go run main.go时在本地工作。我有go.modgo.sum

2 个答案:

答案 0 :(得分:4)

在go项目的根目录中:

go mod init
go mod tidy

提交并推送到github,它现在应该可以工作。

答案 1 :(得分:0)

你需要为 go get 或 go mod 设置一个令牌来下载私有 repos,这就是你得到 404 的原因

然后您需要配置 git 以使用该令牌。

- name: Setup Git
  run: git config --global url."https://${{ secrets.TOKEN }}:@github.com/".insteadOf "https://github.com"

我不使用 dep,但你应该使用 go mod download 而不是 go get 或 dep,因为你有一个 mod 文件。