1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| package main
import ( "fmt" "math" "unicode/utf8" )
func main() { fmt.Printf("3 ways to declare and init a variable:\n")
var a int = 100 var b = "100" c := 0.17
fmt.Printf("a value = %v (%T)\n", a, a) fmt.Printf("b value = %v (%T)\n", b, b) fmt.Printf("c value = %v (%T)\n", c, c) fmt.Println()
fmt.Printf("Easy way to swap variables:\n") v1 := 1 v2 := 2 fmt.Printf("before swap: v1 = %v (%T), v2 = %v (%T)\n", v1, v1, v2, v2)
v1, v2 = v2, v1 fmt.Printf("after swap: v1 = %v (%T), v2 = %v (%T)\n", v1, v1, v2, v2) fmt.Println()
var vUint16 uint16 = math.MaxUint8 + 1 fmt.Printf("vUint16 = %v (%T)\n", vUint16, vUint16)
var vUint8 uint8 = uint8(vUint16) fmt.Printf("vUint8 = %v (%T)\n", vUint8, vUint8)
var vFloat32 float32 = math.E var vFloat64 float64 = math.E fmt.Printf("vFloat32 = %f (%T)\n", vFloat32, vFloat32) fmt.Printf("vFloat64 = %.10f (%T)\n", vFloat64, vFloat64)
var vBool bool = true fmt.Printf("vBool = %v (%T)\n", vBool, vBool)
var vStr string = "你好, Go!" fmt.Printf("vStr = \"%s\" (%T)\n", vStr, vStr) fmt.Printf("byte len of vStr = %v\n", len(vStr)) fmt.Printf("rune len of vStr = %v\n", utf8.RuneCountInString(vStr)) for i, h := range vStr { fmt.Printf("[%v:%c]", i, h) } fmt.Println()
var ptrStr *string = &vStr fmt.Printf("ptrStr = %v (%T)\n", ptrStr, ptrStr) fmt.Printf("*ptrStr = %v (%T)\n", *ptrStr, *ptrStr)
var vStruct struct { id int name string salary float32 } vStruct.id = 1 vStruct.name = "Mike" vStruct.salary = 123.45 fmt.Printf("vStruct = %v (%T)\n", vStruct, vStruct) }
|