代码之家  ›  专栏  ›  技术社区  ›  MafMal

Go Project子目录(无需模块提供包)

go
  •  -1
  • MafMal  · 技术社区  · 2 年前

    我尝试使用子目录(使用 internals ). 目录结构如下:

    根目录: /Users/xyz/code/ppPrinter

    .
    ├── go.mod
    ├── internal
    │   ├── backends
    │   │   └── bar.go
    │   └── commands
    └── main.go
    

    Go mod:

    module bitbucket.org/foo/ppprint.git
    
    go 1.22.5
    

    main.go:

    package main
    
    import (
        "fmt"
    
        be "bitbucket.org/foo/ppPrint.git/internal/backends"
    )
    
    // Hello returns a greeting for the named person.
    func main() {
        // Return a greeting that embeds the name in a message.
        foo := be.Hello("Gladys")
        message := fmt.Sprintf("Hi, %v. Welcome!", name)
        return message
    }
    

    bar.go:

    package backends
    
    import "fmt"
    
    // Hello returns a greeting for the named person.
    func Hello(name string) string {
        // Return a greeting that embeds the name in a message.
        message := fmt.Sprintf("Hi, %v. Welcome!", name)
        return message
    }
    

    在我的系统中,默认情况下设置了no GO*env变量: env | grep GO 什么也不返回。

    正在运行main.go:

    cd $HOME/code/ppPrinter/ && go run main.go
    main.go:6:2: no required module provides package bitbucket.org/foo/ppPrint.git/internal/backends; to add it:
        go get bitbucket.org/foo/ppPrint.git/internal/backends
    

    将GOPATH设置为时出现相同错误 $HOME/code/ppPrinter .
    我缺少什么(我只是想能够在子目录中构建我的本地项目)?

    1 回复  |  直到 2 年前
        1
  •  1
  •   victordobry    2 年前

    模块路径不一致:

    - in `go.mod`:  bitbucket.org/foo/ppprint.git
    - in `main.go`: bitbucket.org/foo/ppPrint.git/internal/backends
    

    更改 ppPrint ppprint .

    如果您在VS Code中工作,您可能需要重新启动Go语言服务器(以消除编辑器中的错误):打开命令面板(Ctrl+Shift+P)并选择“Go:重新启动语言服务器”。