#5 Go 运算符

2020-12-09
  • 算术运算符 Arithmetic Operators
  • + - * / %
  • ++ --
  • 关系运算符 Relational Operators
  • > < == != >= <=
  • 逻辑运算符 Logical Operators
  • && || !
  • 按位运算符 Bitwise Operators
  • & 按位与 bitwise and
  • | 按位或 bitwise or
  • ^ 按位取反/按位异或
  • &^ 与非
  • << 左移,>> 右移
  • 赋值运算符 Assignment Operators
  • = 以及算术运算赋值,位运算赋值
  • 其他运算符 Misc Operators
  • * 指针,& 取地址
优先级 运算符
5 * / % << >> & &^
4 + - | ^
3 关系运算符
2 &&
1 ||

#4 Golang 字符串 string

2020-12-08
package main

func main() {
    a := "hello 世界"
    fmt.Printf("%#v (%d)\n", a, len(a))
    // "hello 世界" (12)


    // 按 rune ForRange 循环
    for k, v := range a {
        fmt.Printf("%#4v. %#6v (%T)\n", k, v, v)
    }
    // 按 byte 索引取值
    fmt.Printf("a[0]: %#6v (%T)\n", a[0], a[0])
    fmt.Printf("a[6]: %#6v (%T)\n", a[6], a[6])
    //    0.    104 (int32)
    //    1.    101 (int32)
    //    2.    108 (int32)
    //    3.    108 (int32)
    //    4.    111 (int32)
    //    5.     32 (int32)
    //    6.  19990 (int32)
    //    9.  30028 (int32)
    // a[0]:   0x68 (uint8)
    // a[6]:   0xe4 (uint8)

    b := []byte(a)
    fmt.Printf("%#v\n", b)
    // []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0xe4, 0xb8, 0x96, 0xe7, 0x95, 0x8c}

    c := []rune(a)
    fmt.Printf("%#v\n", c)
    // []int32{104, 101, 108, 108, 111, 32, 19990, 30028}
}
  • len(s)
  • strings.Join(s1, s2) string
  • strings.ContainsRune(s, rune) bool
  • strings.Contains(s, substr) bool
    注意:任何字符串都包含空字符串
  • strings.Index(s, substr) int
  • strings.Count(s, substr) int
    注意:任何字符串包含 n + 1 个空字符串
  • strings.HasPrefix(s, substr) bool
  • strings.HasSuffix(s, substr) bool
  • strings.Compare(s1, s2) 大于 1 / 小于 -1 / 等于 0
  • strings.Repeat(s, num) string
  • strings.Replace(s, substr, replacement, num) string
  • strings.EqualFold(s1, s2) bool 忽略大小写等于判断
  • strings.Fields(s) 用空白字符(空格、换行、tab)将字符串切割成字串

参考 Golang strings

#1 Go 模板

2020-12-01

fmt.Sprintf 字符串格式化

tpl := `[%s] Your verify code is %s.`
s := fmt.Sprintf(tpl, "Markjour", "1234")
println(s)

os.Expand 变量替换

tpl := `[${sign}] Your verify code is ${code}.`
params := map[string]string{"sign": "Markjour", "code": "1234"}
println(os.Expand(tpl, func(k string) string { return params[k] }))

text/template 和 html/template

这两个就可以处理复杂的情况,嵌套模板,控制语句都支持。

package main

import (
    "os"
    "text/template"
)

func main() {
    tpl := `[{{.sign}}] Your verify code is {{.code}}.`
    t := template.New("just-a-name")
    t, _ = t.Parse(tpl)
    params := map[string]string{"sign": "Markjour", "code": "1234"}
    t.Execute(os.Stdout, params)
}

附:strings.Map / bytes.Map 提供单个字符的替换

func Map(mapping func(rune) rune, s string) string
package main

import (
    "fmt"
    "strings"
)

const A = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ \n"
const B = "N'|&4:@ j{BI+Y!H/Q_iR\\FM}$moLe?#X\"WCE3S,8(r1f%T.;6DbaG]y`q~ltJxu-k2gA\nvhnd=)*s7Z5p^OK[V0z>9<UcwP"

func main() {
    encrypt := func(r rune) rune {
        if !strings.ContainsRune(A, r) {
            return 0
        }
        return rune(B[strings.IndexRune(A, r)])
    }
    decrypt := func(r rune) rune {
        if !strings.ContainsRune(B, r) {
            return 0
        }
        return rune(A[strings.IndexRune(B, r)])
    }

    raw := "Life was like a box of chocolate, you never know what you're gonna get."
    fmt.Println(raw)

    encrypted := strings.Map(encrypt, raw)
    fmt.Println(encrypted)
    // 3j:4wFN_wIjB4wNw'!Mw!:w| !|!INi4dw}!RwY4\4QwBY!FwF Niw}!RAQ4w@!YYNw@4i)

    decrypted := strings.Map(decrypt, encrypted)
    fmt.Println(decrypted)
}

参考资料与拓展阅读