Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/falling-ts/go-import
Learn go import sequence
https://github.com/falling-ts/go-import
Last synced: 7 days ago
JSON representation
Learn go import sequence
- Host: GitHub
- URL: https://github.com/falling-ts/go-import
- Owner: falling-ts
- Created: 2023-02-18T05:43:41.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-17T13:21:49.000Z (2 months ago)
- Last Synced: 2024-10-19T18:40:59.615Z (2 months ago)
- Language: Go
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Go 包的导入顺序
这是关于 Go 包复杂导入时, 导入顺序的学习
### 下面是执行的结果
```shell
f
e
c
g
d
a
h
b
main
exec main
```### 代码示例
```go
package aimport (
"fmt"
_ "import/c"
_ "import/d"
)var name = "a"
func init() {
fmt.Println(name)
}
```### 关于网络上的导入图示
![图片](https://tse3-mm.cn.bing.net/th/id/OIP-C.vdC_CSJUyoIEyqqf4wNPuAHaCq?pid=ImgDet&rs=1)
### 结论
以项目为例, 导入顺序如下
> main->a->c->e->f
>
> d->g
>
> b->h因此 init 执行顺序为
> f->e->c->g->d->a->h->b->main->exec main
```shell
注: 修改导入顺序后, init 执行顺序也跟着变化了
```