执行go test 也会先执行init()
init.go
package testinit
import (
"fmt"
)
func init(){
fmt.Println("initing........")
}
hello.go
package testinit
import "fmt"
func hello(){
fmt.Println("!!!!!hello")
}
hello_test.go
package testinit
import (
"fmt"
"testing"
)
func TestHello(t *testing.T){
fmt.Println("test begining....")
hello()
}
out :
PS D:\goproject\src\first\2020-9-17\testinit> go test
initing........
test begining....
!!!!!hello
PASS
ok first/2020-9-17/testinit 0.369s