review

  1. base64编码
package main
import (
    "encoding/base64"
    "fmt"
)

func main(){
    fmt.Printf("%v\n", base64.StdEncoding.EncodeToString([]byte("user:open sesame")))

    println("done")
}
  1. 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)
}
  1. 使用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))
  1. 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)
    }
}
  1. 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

  1. channel不能被关闭两次(and more)
package main

import (
    "fmt"
)

func main(){
    defer fmt.Println("bye")
    ch := make(chan struct{}, 1)
    close(ch)
    close(ch)
}
  1. 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”这个对象了。 (左值)

  1. 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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值