Go 語言筆記(3) — Data types(上)

Jade Chen
Jade Chen
Dec 18, 2018 · 13 min read

「如果program structure是程式之骨,那麼,Data就是鑲嵌其間的肉」

Photo by Kristian Strand on Unsplash

【導言】


【開發環境】


【六道菜吃到飽】


【壹、Integers】

Precedence(Operator的優先權)

*      /      %      <<       >>     &       &^
+ - | ^
== != < <= > >=
&&
||

Overflow / Underflow

// Example of overflow
var i int8 = 127 // 01111111, bigest number of int8
fmt.Println(i+1) // 10000000, "-128"
fmt.Println(i*i) // 00000001, "1"

用signed或unsigned的時機

medals := []string{"gold", "silver", "bronze"}
for i := len(medals) - 1; i >= 0; i-- {
fmt.Println(medals[i]) // "bronze", "silver", "gold"
}

【貳、Floating-point numbers】

More about “math” library

var z1 float64 // 還記得嗎?此時z1=0歐 
var z2 float64 = 1
fmt.Println(z1, -z1, z2/z1, -z2/z1, z1/z1, math.Sqrt(-z2))
// 0 -0 +Inf -Inf NaN NaN
// z1 == -z1 是true歐
func compute() (value float64, ok bool) {
// ...
if failed {
return 0, false
}
return result, true
}

【參、Complex Numbers】

起手式

var x complex128 = complex(4, 3) // 4+3i
y := 2 + 1i // 2+1i y默認是complex128
fmt.Println(x*y) // "(5+10i)"
fmt.Println(real(x*y)) // "5"
fmt.Println(imag(x*y)) // "10"
// 更多關於complex的運算,請import math/cmplx 包~

【肆、Booleans】


【伍、Strings】

起手式

s1 := "hello, world"               
s2 := "hello, 世界"
fmt.Println(len(s1)) // "12"
fmt.Println(len(s2)) // "13"
fmt.Println(s1[0], s1[8]) // "104 111"
fmt.Println(s2[0], s2[8]) // "104 184"
fmt.Println(s1[:]) // "hello, world"
fmt.Println(s2[:5]) // "hello"
fmt.Println(s2[7:]) // "世界"

String Literal

Raw String Literal

const greeting = `Hello, I'm
RAW
STRING
Literal!!! `

Unicode和UTF-8

0xxxxxxx                             runes 0-127    (ASCII)
110xxxxx 10xxxxxx 128-2047 常用
1110xxxx 10xxxxxx 10xxxxxx 2048-65535 次常用
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 65536-0x10ffff 其他字符
"世界"
"\xe4\xb8\x96\xe7\x95\x8c"
"\u4e16\u754c" // 16bit representation
"\U00004e16\U0000754c" // 32bit representation

Strings和Byte Slices

Conversions between Strings and Numbers

a := 111
fmt.Println(strconv.Itoa(a)) // "111"
fmt.Println(strconv.FormatInt(int64(a), 2)) // 要先轉int64! "1101111"
b, err := strconv.Atoi("123") // b is an int 123
c, err := strconv.ParseInt("123", 10, 64) // base 10, up to 64 bits
b := fmt.Sprintf("a=%b", a)
c := fmt.Sprintf("%d", a)
fmt.Println(b, c) // "a=1101111 111"

【陸、Constants】

起手式

// const yourConstant (type) = value// Just approximate value
const e = 2.7182818284
// or just in one const declaration...
const (
e = 2.7182818284
pi = 3.1415926535
phi = 1.6180339887
)

iota: constant 產生器

type Month intconst (
January Month = iota + 1 // 1
February // 2
March // 3, and so on
April
May
June
July
August
September
October
November
December
)
const (
Byte = 1 << (10 * iota)
KiB // 1024
MiB // 1048576
GiB // 1073741824
TiB // 1099511627776
PiB // 1125899906842624
EiB // 1152921504606846976
ZiB // 1180591620717411303424 (exceeds 1 << 64)
YiB // 1208925819614629174706176
)

Untyped constants

var a float32 = math.Pi
var b complex64 = math.Pi
// ...

【結論】


餐後點心

整個程式都是我的咖啡館

Python和Golang的教學Blog

Thanks to Jack Shiba.

Jade Chen

Written by

Jade Chen

NTUEE119 | Blockchain TA | NTU Sunrise

整個程式都是我的咖啡館

Python和Golang的教學Blog