go语言中import不允许循环包含

前端之家收集整理的这篇文章主要介绍了go语言中import不允许循环包含前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

go的包不允许循环包含,具体例子:

main.go:

package main

import (
    "fmt"
    "test/pkg1"
)

func main() {
    fmt.Println("in main.main")
    fmt.Printf("pkg1.Black=%s\n",pkg1.Black)
    fmt.Printf("pkg2.Black=%s\n",pkg2.Black)
}

func init() {
    fmt.Println("in main.init")
    fmt.Printf("pkg1.Black=%s\n",pkg2.Black)
}

pkg1.go:

package pkg1

import (
    "fmt"
    "test/pkg2"
)

const (
    Black string = "#000"
    white string = "#fff"
)

func init() {
    fmt.Println("in pkg1.init")
    fmt.Printf("pkg2.Black=%s\n",pkg2.Black)
}

pkg2.go:

package pkg2

import (
    "fmt"
    "test/pkg1"
)

const (
    Black string = "#000"
    white string = "#fff"
)

func init() {
    fmt.Println("in pkg2.init")
    fmt.Printf("pkg1.Black=%s\n",pkg1.Black)
}

go build报错:

import cycle not allowed
原文链接:https://www.f2er.com/go/190424.html

猜你在找的Go相关文章