- go make new 区别:
http://www.jb51.net/article/56837.htm
总结一下就是
make 仅用于在slice map chan
new 用在struct
make 指定长度,和reserve 的内存,返回slice map chan类型
new 返回指针
- go struct
struct 基础:
https://www.jianshu.com/p/7b523f8ab141
struct json 转换:
https://www.2cto.com/kf/201710/688646.html
一些需要注意的地方:
https://www.jianshu.com/p/5e7927548ec8
成员函数是否改变对象的值只和receiver 是否为指针类型 有关
匿名结构体定义:
var x = struct {
A string
B int
}{"a",2}
fmt.Println(x)
var y struct {A int;B int}
var z = new(struct{A int;B int})
fmt.Println(y)
fmt.Println(z)
struct的标签 和xml 解析:
https://blog.csdn.net/fyxichen/article/details/47318009
- 打印
//print struct
fmt.Printf("%#v\n",obj) // class name + field name
fmt.Printf("%+v\n",obj) // field name
- slice 截取 时的第三个参数, 并不是其他语言(python)中间隔的意思
参考:http://www.jb51.net/article/56828.htm
func sliceLenTest(){
s:=make([]int,0,5)
//s[10]=1//runtime error
fmt.Println(s)
for i:=0;i<10;i+=1{
s=append(s,i)
}
fmt.Println(s)
fmt.Printf("%#v\n",s)
fmt.Println("without third num")
s1:=s[3:6]
fmt.Println(s1)
s1[0]=10
fmt.Println(s1)
fmt.Println(s)
s1=append(s1,10)
fmt.Println(s1)
fmt.Println(s)
fmt.Println(&s[3],&s1[0],&s[6],&s1[3])
//if you want to keep origin slice unchanged
for i:=0;i<10;i+=1{
s[i]=i
}
fmt.Println("with third num")
//s2:=s[3:6:100]//runtime error
s2:=s[3:6:6]
fmt.Println(s1)
s2[0]=20 //change s
fmt.Println(s2)
fmt.Println(s)
fmt.Println(&s[3],&s2[0])
s2=append(s2,20) //here S2 has been move to another place
s2[1]=20 //will not change s from now on
fmt.Println(s2)
fmt.Println(s)
fmt.Println(&s[3],&s2[0],&s[6],&s2[3])
}
/* output
[]
[0 1 2 3 4 5 6 7 8 9]
[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
without third num
[3 4 5]
[10 4 5]
[0 1 2 10 4 5 6 7 8 9]
[10 4 5 10]
[0 1 2 10 4 5 10 7 8 9]
0xc420098018 0xc420098018 0xc420098030 0xc420098030
with third num
[3 4 5 6]
[20 4 5]
[0 1 2 20 4 5 6 7 8 9]
0xc420098018 0xc420098018
[20 20 5 20]
[0 1 2 20 4 5 6 7 8 9]
0xc420098018 0xc42007a150 0xc420098030 0xc42007a168
*/
-
array slice map 详细说明
http://www.jb51.net/article/56828.htm
slice 内存
https://www.cnblogs.com/bonelee/p/6862377.html
slice 指针的使用情形:
https://studygolang.com/articles/2228
(这篇的最后一个例子,还有copy时时要注意 dest 的 slice 的长度,这篇前面几个例子还可以看出slice的容量大概是2倍2倍这样增长的) -
map 排序
根据value排序:
https://stackoverflow.com/questions/18695346/how-to-sort-a-mapstringint-by-its-values
这个更全一些,另外有关于stable的说明:
参考,第一个和第二个答案,第三个答案是错的
https://studygolang.com/articles/10530 -
go 没有
static
,可以用函数闭包代替
func RemoveTagClosure() func(string)string{
var tag_br = regexp.MustCompile(`(<br>)|(<br/>)|(<BR>)|(<BR/>)`)
var tag_p = regexp.MustCompile(`<[pP]>([^<]*)</[pP]>`)
var tags = regexp.MustCompile(`<\s*/?\s*[a-zA-Z0-9]+.*?>`)
replace :=func (htmlstring string)string{
htmlstring = tag_br.ReplaceAllString(htmlstring, "\n")
htmlstring = tag_p.ReplaceAllString(htmlstring, "$1\n")
htmlstring = tags.ReplaceAllString(htmlstring, "")
return htmlstring
}
return replace
}
var RemoveTag = RemoveTagClosure()
参考:https://stackoverflow.com/questions/30558071/static-local-variable-in-golang
-
单例模式可以用
once.Do()
实现
参考:
https://blog.csdn.net/qibin0506/article/details/50733314 -
go 字符串只有一个进程读,其他写,是否要加锁(感觉是要加的)
https://studygolang.com/articles/2400
关于文章底下的回复,
我觉得,imutable 是指对外的接口是imtable,也就是在写程序的时候不能出现str[0]="a"这种,但是,它不能保证在底层执行时的原子性,也就是go底层依然可以只改变了部分的字符串,然后读到的是只改变了部分的值。
但是稍微测试了一下出现冲突的情景并没有发生。。。也许那个回复是对的。。。
go的引用结构特别烦,string这种你直接去它的地址,得到的是结构体的指针,并不是想c中直接是字符串的开头地址,所以赋值过程中可能也只是直接改变结构体中的address的内容。可能是这个原因。
这个问题不想了,以后在说吧。。。
然后是关于原子操作的一篇:
https://blog.csdn.net/liuxinmingcode/article/details/50095615
既然int都有store这种原子操作,是不是说明string那里一定要加锁呢?
-
sha1 base64
https://studygolang.com/articles/2873 -
go导入包时
.
和_
_
只执行init,不引入包的其他东西
.
相当于usenamespace,可以直接使用包中的东西,不再需要加上包名
参考:https://blog.csdn.net/chydn/article/details/78111248 -
常量自增初始化用itoa
package main
import "fmt"
const (
a = 1<<iota
b
c = iota*2
d
e = iota+100
f
)
func main(){
fmt.Println(a,b,c,d,e,f) //1 2 4 6 104 105
}
- 字符串中的转义字符按字面输出
fmt.Println(strconv.Quote( "\n"))
fmt.Println(`\n`)// string 字符不转义
fmt.Println("\n")// string 可以和[]byte []rune 转换
fmt.Println('\n')// rune int32的别名
- rune 和 byte 的区别
type byte uint8
type rune int32
- rune相当于其他语言中的 char(wchar?),所以单引号中间只能有一个字符,但是 print 会打印字符对应的整数值,这点和 C 差不多
var r rune='?'
fmt.Println(r) //128055
fmt.Printf("%c\n",r) //?
- 遍历 string 得到的是rune, 同时注意此时的下标是按byte计算的下标
for i,c :=range "我是猪" {
fmt.Printf("%d,%T,%c\n",i,c,c)
}
/*
0,int32,我
3,int32,是
6,int32,猪
*/
- 但是string 的 len 是按 byte 算的
fmt.Println(len("我是猪")) //9
fmt.Println(len([]rune("我是猪"))) //3
fmt.Println(utf8.RuneCountInString("我是猪")) //3