Go - Boolean and Strings


Boolean
  • bool
  • boolean
Strings
  • Conventionally UTF-8
  • Built-in function len returns the number of bytes, not runes in a string
  • Detect length of string
    • Convert to []rune and use len()
    • Use unicode/utf8
  • substring, format: string[startIdx:endIdx], need to be reminded you need cast to rune if string contains 
  • Can use + to a append string. 
  • Use == or > or < to compare string byte by byte (natural lexicographic ordering)
  • string is immutable, so you CAN NOT assign value to a char of a string
  • Natural data type to hold a single rune is int32
  • UTF8
    • UTF8 is a variable-length encoding of unicode code points as byte.
    • High order bits of the first byte of the encoding for a rune indicate how many bytes follow. 
    • You can search for rune by searching for its bytes without worrying about the preceding context
    • Lexicographic order equals to unicode point order. So sort UTF8 works naturally
    • Go files always encoded in UTF8
    • For special character. Use \uhhhh for 16 bits value. Usev\Uhhhhhhhh for 32 bits value
    • Can use ==, <, > ...etc to compare string
  • Count characters
    • utf8.RuneCountInString and utf8.DecodeRuneInString
    • Range byte array


沒有留言:

張貼留言

別名演算法 Alias Method

 題目 每個伺服器支援不同的 TPM (transaction per minute) 當 request 來的時候, 系統需要馬上根據 TPM 的能力隨機找到一個適合的 server. 雖然稱為 "隨機", 但還是需要有 TPM 作為權重. 解法 別名演算法...