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

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

GO crifan 4136浏览 0评论

【背景】

折腾:

【记录】用go语言实现模拟登陆百度

期间,学到很多go的东西。

现整理如下:

 

其中,关于go语言的学习参考资料,已整理至:

【整理】go语言的学习参考资料

 

go语言学习心得

 

用go语言实现的项目

go官网的google code上:

http://code.google.com/p/go-wiki/wiki/Projects

列出了,用go语言实现的各种项目。

其中,就有我在

【记录】go语言中通过log4go实现同时输出log信息到log文件和console

中,刚要打算去用的:

log4go – Go logging package akin to log4j

 

对于不关心的函数返回值可用下划线忽略掉

go代码中,调用函数返回值,如果不关心某个返回值,想要忽略某个返回值,可以用下划线,即可实现对应的效果。

比如:

_, fulleFilename, _, _ := runtime.Caller(0)

对应着函数Caller的语法是:

http://golang.org/pkg/runtime/#Caller

func Caller

func Caller(skip int) (pc uintptr, file string, line int, ok bool)

很明显,此处就是:

只把对应的file的返回值给了fulleFilename,而忽略掉了pc,line,ok三个返回值。

详见:

【已解决】go语言运行出错:# command-line-arguments .\EmulateLoginBaidu.go:17: line declared and not used

 

go语言常见问题及解答

 

出现# command-line-arguments时,需要解决此警告(错误),程序才能正常执行

使用如下代码:

package main

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

func main() {
    fmt.Printf("this is EmulateLoginBaidu.go\n")
    
    _, filename, line, _ := runtime.Caller(0)
    fmt.Println(filename)
    fmt.Println(line)
}

去运行,会出错:

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: "os"
.\EmulateLoginBaidu.go:7: imported and not used: "io/ioutil"
.\EmulateLoginBaidu.go:8: imported and not used: "net/http"

很明显,没有正常运行,没有打印出对应的字符串。

需要去消除此些(警告)错误,即把代码改为:

package main

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

func main() {
    fmt.Printf("this is EmulateLoginBaidu.go\n")
    
    _, filename, line, _ := runtime.Caller(0)
    fmt.Println(filename)
    fmt.Println(line)
}

然后再去运行,就可以正常执行了:

D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu>go run EmulateLoginBaidu.go
this is EmulateLoginBaidu.go
D:/tmp/tmp_dev_root/go/src/github.com/user/EmulateLoginBaidu/EmulateLoginBaidu.go
15

 

详见:

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

 

xxx declared and not used

某个变量声明了(赋值了),但是(后面)却没有使用到。

出现此问题的原因很简单:

就是某个变量,定义了,赋值了,但是后面却没有用到。

而对于go语言来说,检测到此,警告后,就会报错。

导致程序无法继续编译,无法运行。

解决办法很简单:

注释掉该变量,不使用该变量,即可。

 

详见:

【已解决】go语言运行出错:# command-line-arguments .\EmulateLoginBaidu.go:17: line declared and not used

 

imported and not used: "xxx"

某某模块,导入了,但是没有使用。

和上面的“xxx declared and not used”很类似。

意思很明显:

某个模块,你导入了,但是没用到,

go语言也会提示你此警告。

导致程序无法继续编译和运行。

解决办法是:

注释掉,删除掉,对应的导入的模块,即可。

 

详见:

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

 

can’t find import: "builtin"

builtin模块,的确是go语言自带的:

http://golang.org/pkg/builtin/

但是,其实该页面中,已经解释了:

The items documented here are not actually in package builtin but their descriptions here allow godoc to present documentation for the language’s special identifiers.

即,本身不存在builtin这个包,所以你是没法用:

import "builtin"

去导入builtin的。

即:

不用import这builtin,而本身可以直接使用了。

比如:

我想要声明一个error,则直接用:

var err error;

即可,无需写成:

var err builtin.error;

总之:

没有import builtin,而直接可以使用即可。

转载请注明:在路上 » 【整理】go语言学习心得和注意事项

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.176 seconds, using 22.07MB memory