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", pkg1.Black)
    fmt.Printf("pkg2.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

李维
270 声望8 粉丝

php/golang程序员,钟爱javascript,向往服务器端开发