【问题】
折腾:
期间,使用代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package main import ( "fmt" "log" "io/ioutil" "net/http" ) func main() { fmt.Printf( "this is EmulateLoginBaidu.go\n" ) //var baiduMainUrl string //baiduMainUrl = "http://www.baidu.com/"; //baiduMainUrl := "http://www.baidu.com/"; fmt.Printf( "baiduMainUrl=%s\n" , baiduMainUrl) //res, _, err := http.Get("http://bbs.golang-china.org/") } |
结果出错:
1 2 3 4 | D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu>go run EmulateLoginBaidu.go # command-line-arguments .\EmulateLoginBaidu.go:5: imported and not used: "log" .\EmulateLoginBaidu.go:6: imported and not used: "io/ioutil" |
即:
imported and not used: "xxx" |
【解决过程】
1.去试试,把对应模块注释掉:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package main import ( "fmt" //"log" //"io/ioutil" //"net/http" ) func main() { fmt.Printf( "this is EmulateLoginBaidu.go\n" ) //var baiduMainUrl string //baiduMainUrl = "http://www.baidu.com/"; //baiduMainUrl := "http://www.baidu.com/"; fmt.Printf( "baiduMainUrl=%s\n" , baiduMainUrl) //res, _, err := http.Get("http://bbs.golang-china.org/") //res, err := http.Get("http://bbs.golang-china.org/") } |
然后就可以了,就不会出错了。程序就可以继续编译和运行了:
1 2 3 | D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu>go run EmulateLoginBaidu.go this is EmulateLoginBaidu.go baiduMainUrl=http://www.baidu.com/ |
如图:
【总结】
当go语言中,import了某个模块后,但是却没使用,就会导致:
imported and not used: "xxx" |
的错误,解决办法是:
注释(去掉)对应的模块,即可:
使得程序正常继续编译,正常运行。
(否则,出现此类警告,会导致不能继续编译和运行的,详见:
)