golang 编译运行出错:imported and not used: "log"

【问题】

使用代码:

?

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

     

    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

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

}

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

?

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"

的错误,解决办法是:

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

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

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值