【背景】
折腾:
期间,需要用到字符串变量。
【解决过程】
1.参考:
去试试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package main import ( "fmt" "log" "io/ioutil" "http" ) func main() { fmt.Printf( "this is EmulateLoginBaidu.go\n" ) fmt.Printf(baiduMainUrl) } |
结果是出错了:
【已解决】go语言运行出错:EmulateLoginBaidu.go:7:5: cannot find package "http" in any of
2.解决后,代码变为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package main import ( "fmt" "log" "io/ioutil" "net/http" ) func main() { fmt.Printf( "this is EmulateLoginBaidu.go\n" ) fmt.Printf(baiduMainUrl) } |
错误变成了:
1 2 3 4 5 6 7 8 9 10 | 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" .\EmulateLoginBaidu.go:12: undefined: baiduMainUrl .\EmulateLoginBaidu.go:12: cannot assign to baiduMainUrl .\EmulateLoginBaidu.go:13: undefined: baiduMainUrl .\EmulateLoginBaidu.go:14: assignment count mismatch: 3 = 2 D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu> |
很明显,此处的字符串变量baiduMainUrl是非法的。
3.所以再去改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package main import ( "fmt" "log" "io/ioutil" "net/http" ) func main() { fmt.Printf( "this is EmulateLoginBaidu.go\n" ) var baiduMainUrl string fmt.Printf(baiduMainUrl) } |
结果是
1 2 3 4 5 | 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" .\EmulateLoginBaidu.go:16: assignment count mismatch: 3 = 2 |
4.再去参考:
改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package main import ( "fmt" "log" "io/ioutil" "net/http" ) func main() { fmt.Printf( "this is EmulateLoginBaidu.go\n" ) var baiduMainUrl string fmt.Printf(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" |
5.即没有了错误了,但是却没运行出来结果。
参考:
去改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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/"; fmt.Printf( "baiduMainUrl=%s\n" , baiduMainUrl) //res, _, err := http.Get("http://bbs.golang-china.org/") } |
结果是:
1 2 3 4 5 | 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" .\EmulateLoginBaidu.go:15: no new variables on left side of := |
6.对于imported and not used的错误,详见:
【已解决】go语言编译运行出错:imported and not used: "log"
【总结】
至此,至少先确保了:
可以通过:
1 |
方式,去初始化一个字符串值,并且赋值的。
转载请注明:在路上 » 【已解决】go语言中的字符串