为什么我的 GitHub Actions 脚本找不到我的内部 go 包?

Why does my GitHub Actions script cannot find my internal go package?

我正在学习 Go 并想使用 GitHub Actions。只使用一个包时一切都很好。但是一旦我定义了多个包(多于主包),我就卡住了。在我的桌面上它确实可以编译,但是通过使用 Actions 脚本它不会编译并最终出现以下错误:

Run go build -v main.go
main.go:4:2: cannot find package "Landsat-Extractor/logger" in any of:
    /opt/hostedtoolcache/go/1.14.4/x64/src/Landsat-Extractor/logger (from $GOROOT)
    /home/runner/go/src/Landsat-Extractor/logger (from $GOPATH)
##[error]Process completed with exit code 1.

文件结构为:

go
└───src
    └───Landsat-Extractor
        │   main.go
        │
        └───logger
        |   │   logger.go
        |
        └───.github
            └───workflows
                |   go.yml

在我的本地机器上,GOPATH 设置为先前文件结构的 go

我的操作脚本 go.yml 是:

name: Go

on:
  push:
    branches: [ master, feature_githubaction ]

jobs:

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

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

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

    - name: Build
      run: go build -v main.go

main.go是:

package main

import (
    "Landsat-Extractor/logger"
)

func main() {

    logger.Create()
    defer logger.Destroy()

    logger.Info("A message")

}

logger.go是:

package logger

// Create inits the Logger
func Create() {
    println("Creating")
}

// Info logs a message
func Info(msg string) {
    println(msg)
}

// Destroy closes writers
func Destroy() {
    println("Closing")
}

~/go/src/Landsat-Extractor运行go mod init

这将有助于解决您的模块导入问题。