1 国内如何快速go get github的包
在命令行输入go get ...
前输入以下命令
#配置你自己的http代理
export http_proxy=http://127.0.0.1:1087
go get ...
2 golang版本jieba的效率问题
golang上最出名的github.com/yanyiwu/gojieba
分词器,线程安全,只需要new一次就可以了
x := gojieba.NewJieba()
x.CutForSearch(s, !use_hmm)
3 string包下的strings.Compare的问题
该方法返回值,不是只有1
和0
而是1
,0
,-1
三种情况
需要用下面的语句进行判断不相等:
if strings.Compare(a, b)!=0{
...
4 slice是值复制,不是值引用(万年巨坑)
slice进行append
时,会copy一份源对象,扔到slice
中,而是把引用扔进去
这就意味着,你在append
你的对象之后,你在外部改变,不会影响到slice
中的元素
如果你想把引用扔进去,请使用指针
var NeedArticleList = []*po.Article{}
NeedArticleList = append(NeedArticleList, &article)
5 迭代器中使用指针,需要第三方变量进行指向,否则指针永远是一个
如果你使用下面的代码,那么当循环结束时,NeedArticleList
中的所有元素,都将指向最后一个元素。
var NeedArticleList = []*po.Article{...}
for _, article := range articleList {
NeedArticleList = append(constant.NeedArticleList, article)
}
正确使用方式:
var NeedArticleList = []*po.Article{...}
for index, article := range articleList {
temp:=article
NeedArticleList = append(constant.NeedArticleList, temp)
}