最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】go语言编译运行出错:imported and not used: "log"

GO crifan 8051浏览 0评论

【问题】

折腾:

【已解决】go语言中的字符串

期间,使用代码:

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/";
    
    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/")
}

结果出错:

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.去试试,把对应模块注释掉:

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/";
    
    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/")
}

然后就可以了,就不会出错了。程序就可以继续编译和运行了:

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"

的错误,解决办法是:

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

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

(否则,出现此类警告,会导致不能继续编译和运行的,详见:

【整理】go语言学习心得和注意事项

转载请注明:在路上 » 【已解决】go语言编译运行出错:imported and not used: "log"

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
85 queries in 0.211 seconds, using 22.18MB memory