douqin3245 2016-02-22 02:24
浏览 27
已采纳

如何使用嵌套目录进行构建

In the process of learning go I was playing around with making my own libraries. Here is what I did: in my $GOPATH/src I have two folders: mylibs and test. The test folder has a file called test.go which contains

package test

import "mylibs/hi/saysHi"

func main() {
    saysHi.SayHi()
}

The mylibs folder contains another folder called hi, which has a file called saysHi.go containing:

package saysHi

import "fmt"

func SayHi() {
    fmt.Printf("Hi
")
}

So the directory structure looks like this:

  • GOPATH/src
    • test
      • test.go
    • mylibs
      • hi
        • saysHi.go

The problem is that when I try to compile test it complains saying

cannot find package "mylibs/hi/saysHi" in any of: 
    [...]
    $GOPATH/src/mylibs/hi/saysHi (from $GOPATH)

I have deliberately made the directory structure deeper than necessary. If I make a simpler directory structure where I place saysHi.go in $GOPATH/saysHi/saysHi.go then it works.

But I don't see a reason for why this wouldn't work. Any ideas?

  • 写回答

1条回答 默认 最新

  • duanlang1196 2016-02-22 02:33
    关注

    Generally speaking, your directory name should match the package name. So if you define

    package saysHi
    

    and want to import it with

    import "mylibs/hi/saysHi"

    you should place it in a structure like this:

    • mylibs
      • hi
        • saysHi
          • saysHi.go

    The name of the .go file(s) inside the package makes no difference to the import path, so you could call the .go file anything you like.

    To explain it a bit further, the import path you use should be the name of the directory containing the package. But, if you define a different package name inside that directory, you should use that name to access the package inside the code. This can be confusing, so it's best to avoid it until you understand where it's best used (hint: package versioning).

    It gets confusing, so for example, if you had your package in the path

    • mylibs
      • hi
        • saysHi.go

    And inside saysHi.go defined,

    package saysHi
    

    Then in test.go you will import it with

    import "mylibs/hi"
    

    And use it with

    saysHi.SayHi()
    

    Notice how you import it with the final directory being hi, but use it with the name saysHi.

    Final note

    Just in case you didn't know the following: your test file is called test.go, and that's fine, if it's just as an example, and not an actual test file for saysHi.go. But if it is/were a file containing tests for saysHi.go, then the accepted Go standard is to name the file saysHi_test.go and place it inside the same package alongside saysHi.go.

    One more final note

    I mentioned how you are allowed to choose a different package name from the directory name. But there is actually a way to write the code so that it's less confusing:

     import (
        saysHi "mylibs/hi"
     )
    

    Would import it from the mylibs/hi directory, and make a note of the fact that it should be used with saysHi, so readers of your code understand that without having to go look at the mylibs/hi code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。