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

【背景】

折腾:

【记录】用go实现基本http方面的抓取网页html

期间,需要用到字符串变量。

【解决过程】

1.参考:

[go语言]数组和字符串

去试试:

package main

import (
    "fmt"
    "log"
    "io/ioutil"
    "http"
)

func main() {
    fmt.Printf("this is EmulateLoginBaidu.go\n")
    baiduMainUrl = "http://www.baidu.com/";
    fmt.Printf(baiduMainUrl)
    res, _, err := http.Get("http://bbs.golang-china.org/")
}

结果是出错了:

【已解决】go语言运行出错:EmulateLoginBaidu.go:7:5: cannot find package "http" in any of

2.解决后,代码变为:

package main

import (
    "fmt"
    "log"
    "io/ioutil"
    "net/http"
)

func main() {
    fmt.Printf("this is EmulateLoginBaidu.go\n")
    baiduMainUrl = "http://www.baidu.com/";
    fmt.Printf(baiduMainUrl)
    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"
.\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.所以再去改为:

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)
    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"
.\EmulateLoginBaidu.go:16: assignment count mismatch: 3 = 2

4.再去参考:

Package http

改为:

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

5.即没有了错误了,但是却没运行出来结果。

参考:

Exercise: Web Crawler

Effective 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/";
    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"
.\EmulateLoginBaidu.go:15: no new variables on left side of :=

6.对于imported and not used的错误,详见:

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

 

【总结】

至此,至少先确保了:

可以通过:

var baiduMainUrl string = "http://www.baidu.com/";

方式,去初始化一个字符串值,并且赋值的。



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量