- base64编码
package main
import (
"encoding/base64"
"fmt"
)
func main(){
fmt.Printf("%v\n", base64.StdEncoding.EncodeToString([]byte("user:open sesame")))
println("done")
}
- aes加密
// +build ignore
// +build windows
package main
import (
"fmt"
"crypto/aes"
)
func main() {
bc, err := aes.NewCipher([]byte("key2345689012345"))
if err!=nil {
panic(err)
}
fmt.Printf("blocksize = %v\n", bc.BlockSize())
dst := make([]byte, 16)
src := []byte("sensitive1234567") // full block
bc.Encrypt(dst, src)
fmt.Println(dst)
}
- 使用pprof
import _ "net/http/pprof"
go http.ListenAndServe(":8080", nil)
4.各种时间格式
t:=time.Time{}
fmt.Println(t.Format(time.ANSIC))
fmt.Println(t.Format(time.RFC822Z))
fmt.Println(time.Now().Format(time.ANSIC))
fmt.Println(time.Now().Format(time.RFC822Z))
- range中的闭包&map的存储(影响遍历顺序)
func f1() {
m := make(map[int]int)
for i := 1; i < 10; i++ {
m[i] = i
}
l := []func() (int, int){}
for k, v := range m {
//fmt.Printf("%v => %v\n", k, v)
l = append(l, func() (int, int) {
return k, v
})
}
for _, x := range l {
k, v := x()
fmt.Println(k, v)
}
}
- struct的比较
type XStru struct {
Age int `json:"age"`
Tick struct {
Name string `json:"name"`
Alive bool `json:"alive"`
} `json:"tick"`
}
func main() {
var bstr = []byte(`{"age":28, "tick":{"name":"stan","gender":"male", "alive":true}}`)
fmt.Printf("%v\n", string(bstr))
var o0 XStru
if e := json.Unmarshal(bstr, &o0); nil != e {
panic(e)
}
var o1 XStru
if e := json.Unmarshal([]byte(`{"age":28,"tick":{"name":"stan","alive":true}}`), &o1); nil != e {
panic(e)
}
fmt.Printf("o0 == o1 ?? %v\n", o0 == o1)
if w, e := json.MarshalIndent(o0, "", " "); nil == e {
fmt.Printf("%v\n", string(w))
} else {
fmt.Printf("Fail to marhsal:%v\n", e)
}
}
refer to
https://stackoverflow.com/questions/39643251/golang-struct-comparison
- channel不能被关闭两次(and more)
package main
import (
"fmt"
)
func main(){
defer fmt.Println("bye")
ch := make(chan struct{}, 1)
close(ch)
close(ch)
}
- switch type
package main
import (
"fmt"
)
type MyInt int
func main() {
defer fmt.Println("bye")
d := new(MyInt)
var di interface{} = d
switch di.(type) {
case int:
fmt.Println("int")
case MyInt:
fmt.Println("MyInt")
case *int:
fmt.Println("*int")
case *MyInt:
fmt.Println("*myInt")
}
}
package main
import (
"fmt"
)
type presentBool bool
func main() {
defer fmt.Println("bye")
a := new(presentBool)
*a = true
if *a {
fmt.Println("Yes")
} else {
fmt.Println("No")
}
var d interface{} = a
// Exception occurs
//fmt.Printf("d deferenced to %v\n", d.(*bool))
fmt.Printf("d deferenced to %v\n", d.(*presentBool))
}
9.map和(c++stl map的不同)
package main
import (
"fmt"
)
func main() {
m := make(map[string]bool)
m["uno"] = true
fmt.Println(m["yes"])
fmt.Println(m["uno"])
fmt.Println("after testing")
for name, val := range m {
fmt.Printf("%v => %v\n", name, val)
}
}
C++在写作mapObj[“uno”]的时候就会创建”uno”这个对象了。 (左值)
- literal的range的写法
package main
import (
"fmt"
)
func main() {
//arrs := []string{"uno", "dos"}
for _, v := range []string{"tres", "cuatro"} {
fmt.Println(v)
}
}
为了更好的理解:
// +build ignore
package main
import "fmt"
func main() {
for _, v := range []string(nil) {
fmt.Printf("%v\n", v)
}
}
(到63a)