Golang – 包名使用双下划线

huangapple go评论93阅读模式
英文:

Golang - package name as double underscore

问题

在我的生成的Go代码中,包的名称是:

package __

双下划线的含义是什么?它是否与文件夹名称相同?

是否有关于此的文档?我搜索了一下,但没有找到任何相关的文档。而且代码可以编译通过,没有错误。

英文:

In my protobuf generated go code, the package is:

package __

What does the double underscore mean, does it means the same as folder name ?

Is there a document for this, I searched but didn't found any. And the code can compile without error.

答案1

得分: 3

是的,它表示相同的目录。让我们看一下下面的代码。

目录结构

.
├── go.mod
├── greet
│   └── greet.go
└── main.go

greet.go 中的内容

package __

import "fmt"

func Hello(name string) {
    fmt.Printf("Hello %s\n", name)
}

main.go 中的内容

package main

import greet "playground/greet"

func main() {
    greet.Hello("Eric")
}

当前目录

$ pwd
/Users/thedatageek/Codes/go-playground

不幸的是,我也找不到任何关于go的文档。

但是看起来这是一件好事。你真的不需要给包命名。你只需要给目录命名,包名会自动与之相同。

注意: 这绝对不是grpcprotobuf的事情。然而,如果你从proto文件生成了proto存根,并且如果你添加了一些额外的实用文件,你可以将它们放在一个目录中,然后通过目录名直接导入。例如以下的GitHub仓库

https://github.com/Ash110/gRPC-Logger
https://github.com/dist1ll/cache-prototype
https://github.com/kamensotirov99/int-gateway
https://github.com/rachaelyychen/go-gee
https://github.com/suvvm/ToadOCREngine
https://github.com/denyami/drawing-api

英文:

Yes it means the same directory. Let's look at the following code.

Directory Structure

.
├── go.mod
├── greet
│   └── greet.go
└── main.go

Content in greet.go

package __

import "fmt"

func Hello(name string) {
	fmt.Printf("Hello %s\n", name)
}

Content in main.go

package main

import greet "playground/greet"

func main() {
	greet.Hello("Eric")
}

Current Directory

$ pwd
/Users/thedatageek/Codes/go-playground

Unfortunately I also couldn't find any docs for go.

But it seems that its kinda good thing. You really don't need to name the package. You just name the directory and the package name would be automatically the same.

Note: This is definitely not the grpc or protobuf thing. It is however a customary that if you have generated proto stub from a proto file and if you add some additional utility file you may put those into a dir and then import it directly via directory name. For example the following github repos

https://github.com/Ash110/gRPC-Logger
https://github.com/dist1ll/cache-prototype
https://github.com/kamensotirov99/int-gateway
https://github.com/rachaelyychen/go-gee
https://github.com/suvvm/ToadOCREngine
https://github.com/denyami/drawing-api

答案2

得分: 1

我找到关于package __(双下划线)的信息:

  • 当导入这样的包时,必须指定别名,不能省略,否则找不到该包。
  • 当导入时,编辑器(例如goland)无法自动搜索,必须手动导入。

所以,我猜__表示“无名称”或“无默认名称”。


更新 - 最好避免使用它。

在使用__一段时间后,我建议避免使用它。
因为IDE无法通过包名找到它。
这很不方便,特别是当你想要重用包中的函数时。

英文:

What I found about package __ (double underscore):

  • When import such package, must specify alias, can't omit, otherwise can't find the package.
  • When import, editor (e.g goland) can't auto search, must import by hand.

So, I guess __ means no name or no default name.


Update - better avoid it.

After using __ for a while, I would suggest to avoid it.
Because the IDE can't find it by package name.
It's inconvenient, especially when u want to reuse functions in the package.

huangapple
  • 本文由 发表于 2022年11月26日 13:40:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/74579996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定