https://github.com/falling-ts/go-import
Learn go import sequence
https://github.com/falling-ts/go-import
Last synced: 3 months 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 (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-17T13:21:49.000Z (9 months ago)
- Last Synced: 2025-02-09T05:42:25.070Z (5 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)
}
```### 关于网络上的导入图示

### 结论
以项目为例, 导入顺序如下
> main->a->c->e->f
>
> d->g
>
> b->h因此 init 执行顺序为
> f->e->c->g->d->a->h->b->main->exec main
```shell
注: 修改导入顺序后, init 执行顺序也跟着变化了
```