Go语言同包下引用不同文件的全局指针变量为什么为nil呢?

model.go

package db

import (
   "database/sql"
   "fmt"
   _ "github.com/go-sql-driver/mysql"
)

var db *sql.DB
func Init() {
   db, err := sql.Open("mysql", "root@kong@tcp(127.0.0.1:3306)/blog")
   if err != nil {
      fmt.Println(err)
   }
   db.SetMaxIdleConns(50)
   db.Ping()
}


demo.go

package db

import "fmt"

func Print() {
   fmt.Println(db)
}


main.go

func main() {
   db.Init()
   db.Print()
}


结果:

<nil>

街灯下的橱窗
浏览 718回答 0
0回答

慕斯4360584

ICARNSMTWXQAOCNPEZTBLZLINCHIYGUWHDZKTNIQHEFJVCAULFJQEFZUXOUWRYGAAXKJNDEOGWNUYPIYFFJXUBRFIBGNOIPUKBSNQQOIQDBYZZLCVSTXLFPWJIQQNXVTJDNKRBRPZACWWXLKJPNGBQTABODXDALFGULLCICYMMDEKEFWINDXBZFMDDXPCSFJBEIFMIFXFTUIMTZAQKHNFAYVMBMSVVWTDBJQHBLUSSMWOCROICUDQUFGWVMXUOXKHRFWHWDDPYQQFIZTAAXKAQWGKHEVXAYZYVMZWZQRIZTGVFHUVCTXEKRYJZRFFMNVFFWHZDLITDYZGMOPWGNDOQWJHIFWJAAHDDGVVSQBOOIAKURCELJIHPTNJWBSJAXNWUSZGILNAAJWLBIHJJWORVZRRXRWGBVLCJXXNCSPNKVVIWCJTOFCDVWZDSVTUKSFVMMQPXEOYFIWIYZPGTAFTWZKQFDOJDLHTGJDZNRLCEMSGXEVDRKLIISTVUTCGQDNOCMABXTWAHOIYLHNYSISXOFCMFJSSVFZKAONRHXGCZNQIHDKAVSTUAOYVGPZVSCTQWAUSPFMYVISJTKUOOXWRCZQGGZFTSRGXKWSPLBRYKQGTDMTFNJYMQYVEEUELIUPBSBNKELFBRAXBOXLRYVRMJSPJTBREHHFHAORYWQWTPYHXUFPGWXBEKGMDQBKBAESDPXKLKIFEVY

慕斯4360584

SMMMWURULLFLLELVSGNYQNLOEPZOERSZCXDDDLBIRHOSTNOV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