go语言编译运行出错:imported and not used: "log"

来源:互联网 发布:黄鹤财富网络借贷 编辑:程序博客网 时间:2024/05/20 05:26

【问题】

使用代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
packagemain
 
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/";
     
    var baiduMainUrl string = "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
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
packagemain
 
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/";
     
    var baiduMainUrl string = "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/

如图:

not warning can continue run go code

 

【总结】

当go语言中,import了某个模块后,但是却没使用,就会导致:

imported and not used: "xxx"

的错误,解决办法是:

注释(去掉)对应的模块,即可:

使得程序正常继续编译,正常运行。


原创粉丝点击