| 
 | 1 | +// Example code for Chapter 2.2 from "Build Web Application with Golang"  | 
 | 2 | +// Purpose: Goes over the assignment and manipulation of basic data types.  | 
 | 3 | +package main  | 
 | 4 | + | 
 | 5 | +import (  | 
 | 6 | +	"errors"  | 
 | 7 | +	"fmt"  | 
 | 8 | +)  | 
 | 9 | + | 
 | 10 | +// constants  | 
 | 11 | +const Pi = 3.1415926  | 
 | 12 | + | 
 | 13 | +// booleans default to `false`  | 
 | 14 | +var isActive bool                   // global variable  | 
 | 15 | +var enabled, disabled = true, false // omit type of variables  | 
 | 16 | + | 
 | 17 | +// grouped definitions  | 
 | 18 | +const (  | 
 | 19 | +	i         = 1e4  | 
 | 20 | +	MaxThread = 10  | 
 | 21 | +	prefix    = "astaxie_"  | 
 | 22 | +)  | 
 | 23 | + | 
 | 24 | +var (  | 
 | 25 | +	frenchHello string      // basic form to define string  | 
 | 26 | +	emptyString string = "" // define a string with empty string  | 
 | 27 | +)  | 
 | 28 | + | 
 | 29 | +func show_multiple_assignments() {  | 
 | 30 | +	fmt.Println("show_multiple_assignments()")  | 
 | 31 | +	var v1 int = 42  | 
 | 32 | + | 
 | 33 | +	// Define three variables with type "int", and initialize their values.  | 
 | 34 | +	// vname1 is v1, vname2 is v2, vname3 is v3  | 
 | 35 | +	var v2, v3 int = 2, 3  | 
 | 36 | + | 
 | 37 | +	// `:=` only works in functions  | 
 | 38 | +	// `:=` is the short way of declaring variables without  | 
 | 39 | +	//  specifying the type and using the keyboard `var`.  | 
 | 40 | +	vname1, vname2, vname3 := v1, v2, v3  | 
 | 41 | + | 
 | 42 | +	// `_` disregards the returned value.  | 
 | 43 | +	_, b := 34, 35  | 
 | 44 | + | 
 | 45 | +	fmt.Printf("vname1 = %v, vname2 = %v, vname3 = %v\n", vname1, vname2, vname3)  | 
 | 46 | +	fmt.Printf("v1 = %v, v2 = %v, v3 = %v\n", v1, v2, v3)  | 
 | 47 | +	fmt.Println("b =", b)  | 
 | 48 | +}  | 
 | 49 | +func show_bool() {  | 
 | 50 | +	fmt.Println("show_bool()")  | 
 | 51 | +	var available bool // local variable  | 
 | 52 | +	valid := false     // Shorthand assignment  | 
 | 53 | +	available = true   // assign value to variable  | 
 | 54 | + | 
 | 55 | +	fmt.Printf("valid = %v, !valid = %v\n", valid, !valid)  | 
 | 56 | +	fmt.Printf("available = %v\n", available)  | 
 | 57 | +}  | 
 | 58 | +func show_different_types() {  | 
 | 59 | +	fmt.Println("show_different_types()")  | 
 | 60 | +	var (  | 
 | 61 | +		unicodeChar rune  | 
 | 62 | +		a           int8  | 
 | 63 | +		b           int16  | 
 | 64 | +		c           int32  | 
 | 65 | +		d           int64  | 
 | 66 | +		e           byte  | 
 | 67 | +		f           uint8  | 
 | 68 | +		g           int16  | 
 | 69 | +		h           uint32  | 
 | 70 | +		i           uint64  | 
 | 71 | +	)  | 
 | 72 | +	var cmplx complex64 = 5 + 5i  | 
 | 73 | + | 
 | 74 | +	fmt.Println("Default values for int types")  | 
 | 75 | +	fmt.Println(unicodeChar, a, b, c, d, e, f, g, h, i)  | 
 | 76 | + | 
 | 77 | +	fmt.Printf("Value is: %v\n", cmplx)  | 
 | 78 | +}  | 
 | 79 | +func show_strings() {  | 
 | 80 | +	fmt.Println("show_strings()")  | 
 | 81 | +	no, yes, maybe := "no", "yes", "maybe" // brief statement  | 
 | 82 | +	japaneseHello := "Ohaiyou"  | 
 | 83 | +	frenchHello = "Bonjour" // basic form of assign values  | 
 | 84 | + | 
 | 85 | +	fmt.Println("Random strings")  | 
 | 86 | +	fmt.Println(frenchHello, japaneseHello, no, yes, maybe)  | 
 | 87 | + | 
 | 88 | +	// The backtick, `, will not escape any character in a string  | 
 | 89 | +	fmt.Println(`This   | 
 | 90 | +	is on  | 
 | 91 | +	multiple lines`)  | 
 | 92 | +}  | 
 | 93 | +func show_string_manipulation() {  | 
 | 94 | +	fmt.Println("show_string_manipulation()")  | 
 | 95 | +	var s string = "hello"  | 
 | 96 | + | 
 | 97 | +	//You can't do this with strings  | 
 | 98 | +	//s[0] = 'c'  | 
 | 99 | + | 
 | 100 | +	s = "hello"  | 
 | 101 | +	c := []byte(s) // convert string to []byte type  | 
 | 102 | +	c[0] = 'c'  | 
 | 103 | +	s2 := string(c) // convert back to string type  | 
 | 104 | + | 
 | 105 | +	m := " world"  | 
 | 106 | +	a := s + m  | 
 | 107 | + | 
 | 108 | +	d := "c" + s[1:] // you cannot change string values by index, but you can get values instead.  | 
 | 109 | +	fmt.Printf("%s\n", d)  | 
 | 110 | + | 
 | 111 | +	fmt.Printf("s = %s, c = %v\n", s, c)  | 
 | 112 | +	fmt.Printf("s2 = %s\n", s2)  | 
 | 113 | +	fmt.Printf("combined strings\na = %s, d = %s\n", a, d)  | 
 | 114 | +}  | 
 | 115 | +func show_errors() {  | 
 | 116 | +	fmt.Println("show_errors()")  | 
 | 117 | +	err := errors.New("Example error message\n")  | 
 | 118 | +	if err != nil {  | 
 | 119 | +		fmt.Print(err)  | 
 | 120 | +	}  | 
 | 121 | +}  | 
 | 122 | +func show_iota() {  | 
 | 123 | +	fmt.Println("show_iota()")  | 
 | 124 | +	const (  | 
 | 125 | +		x = iota // x == 0  | 
 | 126 | +		y = iota // y == 1  | 
 | 127 | +		z = iota // z == 2  | 
 | 128 | +		w        // If there is no expression after constants name,  | 
 | 129 | +		// it uses the last expression, so here is saying w = iota implicitly.  | 
 | 130 | +		// Therefore w == 3, and y and x both can omit "= iota" as well.  | 
 | 131 | +	)  | 
 | 132 | + | 
 | 133 | +	const v = iota // once iota meets keyword `const`, it resets to `0`, so v = 0.  | 
 | 134 | + | 
 | 135 | +	const (  | 
 | 136 | +		e, f, g = iota, iota, iota // e=0,f=0,g=0 values of iota are same in one line.  | 
 | 137 | +	)  | 
 | 138 | +	fmt.Printf("x = %v, y = %v, z = %v, w = %v\n", x, y, z, w)  | 
 | 139 | +	fmt.Printf("v = %v\n", v)  | 
 | 140 | +	fmt.Printf("e = %v, f = %v, g = %v\n", e, f, g)  | 
 | 141 | +}  | 
 | 142 | + | 
 | 143 | +// Functions and variables starting with a capital letter are public to other packages.  | 
 | 144 | +// Everything else is private.  | 
 | 145 | +func This_is_public()  {}  | 
 | 146 | +func this_is_private() {}  | 
 | 147 | + | 
 | 148 | +func set_default_values() {  | 
 | 149 | +	// default values for the types.  | 
 | 150 | +	const (  | 
 | 151 | +		a int     = 0  | 
 | 152 | +		b int8    = 0  | 
 | 153 | +		c int32   = 0  | 
 | 154 | +		d int64   = 0  | 
 | 155 | +		e uint    = 0x0  | 
 | 156 | +		f rune    = 0   // the actual type of rune is int32  | 
 | 157 | +		g byte    = 0x0 // the actual type of byte is uint8  | 
 | 158 | +		h float32 = 0   // length is 4 byte  | 
 | 159 | +		i float64 = 0   //length is 8 byte  | 
 | 160 | +		j bool    = false  | 
 | 161 | +		k string  = ""  | 
 | 162 | +	)  | 
 | 163 | +}  | 
 | 164 | +func show_arrays() {  | 
 | 165 | +	fmt.Println("show_arrays()")  | 
 | 166 | +	var arr [10]int // an array of type int  | 
 | 167 | +	arr[0] = 42     // array is 0-based  | 
 | 168 | +	arr[1] = 13     // assign value to element  | 
 | 169 | + | 
 | 170 | +	a := [3]int{1, 2, 3} // define a int array with 3 elements  | 
 | 171 | + | 
 | 172 | +	b := [10]int{1, 2, 3}  | 
 | 173 | +	// define a int array with 10 elements,  | 
 | 174 | +	// and first three are assigned, rest of them use default value 0.  | 
 | 175 | + | 
 | 176 | +	c := [...]int{4, 5, 6} // use `…` replace with number of length, Go will calculate it for you.  | 
 | 177 | + | 
 | 178 | +	// define a two-dimensional array with 2 elements, and each element has 4 elements.  | 
 | 179 | +	doubleArray := [2][4]int{[4]int{1, 2, 3, 4}, [4]int{5, 6, 7, 8}}  | 
 | 180 | + | 
 | 181 | +	// You can write about declaration in a shorter way.  | 
 | 182 | +	easyArray := [2][4]int{{1, 2, 3, 4}, {5, 6, 7, 8}}  | 
 | 183 | + | 
 | 184 | +	fmt.Println("arr =", arr)  | 
 | 185 | +	fmt.Printf("The first element is %d\n", arr[0]) // get element value, it returns 42  | 
 | 186 | +	fmt.Printf("The last element is %d\n", arr[9])  | 
 | 187 | +	//it returns default value of 10th element in this array, which is 0 in this case.  | 
 | 188 | + | 
 | 189 | +	fmt.Println("array a =", a)  | 
 | 190 | +	fmt.Println("array b =", b)  | 
 | 191 | +	fmt.Println("array c =", c)  | 
 | 192 | + | 
 | 193 | +	fmt.Println("array doubleArray =", doubleArray)  | 
 | 194 | +	fmt.Println("array easyArray =", easyArray)  | 
 | 195 | +}  | 
 | 196 | +func show_slices() {  | 
 | 197 | +	fmt.Println("show_slices()")  | 
 | 198 | +	// define a slice with 10 elements which types are byte  | 
 | 199 | +	var ar = [10]byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}  | 
 | 200 | + | 
 | 201 | +	// define two slices with type []byte  | 
 | 202 | +	var a, b []byte  | 
 | 203 | + | 
 | 204 | +	// a points to elements from 3rd to 5th in array ar.  | 
 | 205 | +	a = ar[2:5]  | 
 | 206 | +	// now a has elements ar[2]、ar[3] and ar[4]  | 
 | 207 | + | 
 | 208 | +	// b is another slice of array ar  | 
 | 209 | +	b = ar[3:5]  | 
 | 210 | +	// now b has elements ar[3] and ar[4]  | 
 | 211 | + | 
 | 212 | +	// define an array  | 
 | 213 | +	var array = [10]byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}  | 
 | 214 | +	// define two slices  | 
 | 215 | +	var aSlice, bSlice []byte  | 
 | 216 | + | 
 | 217 | +	// some convenient operations  | 
 | 218 | +	aSlice = array[:3] // equals to aSlice = array[0:3] aSlice has elements a,b,c  | 
 | 219 | +	aSlice = array[5:] // equals to aSlice = array[5:10] aSlice has elements f,g,h,i,j  | 
 | 220 | +	aSlice = array[:]  // equals to aSlice = array[0:10] aSlice has all elements  | 
 | 221 | + | 
 | 222 | +	// slice from slice  | 
 | 223 | +	aSlice = array[3:7]  // aSlice has elements d,e,f,g,len=4,cap=7  | 
 | 224 | +	bSlice = aSlice[1:3] // bSlice contains aSlice[1], aSlice[2], so it has elements e,f  | 
 | 225 | +	bSlice = aSlice[:3]  // bSlice contains aSlice[0], aSlice[1], aSlice[2], so it has d,e,f  | 
 | 226 | +	bSlice = aSlice[0:5] // slcie could be expanded in range of cap, now bSlice contains d,e,f,g,h  | 
 | 227 | +	bSlice = aSlice[:]   // bSlice has same elements as aSlice does, which are d,e,f,g  | 
 | 228 | + | 
 | 229 | +	fmt.Println("slice ar =", ar)  | 
 | 230 | +	fmt.Println("slice a =", a)  | 
 | 231 | +	fmt.Println("slice b =", b)  | 
 | 232 | +	fmt.Println("array =", array)  | 
 | 233 | +	fmt.Println("slice aSlice =", aSlice)  | 
 | 234 | +	fmt.Println("slice bSlice =", bSlice)  | 
 | 235 | +	fmt.Println("len(bSlice) =", len(bSlice))  | 
 | 236 | +}  | 
 | 237 | +func show_map() {  | 
 | 238 | +	fmt.Println("show_map()")  | 
 | 239 | +	// use string as key type, int as value type, and you have to use `make` initialize it.  | 
 | 240 | +	var numbers map[string]int  | 
 | 241 | +	// another way to define map  | 
 | 242 | +	numbers = make(map[string]int)  | 
 | 243 | +	numbers["one"] = 1 // assign value by key  | 
 | 244 | +	numbers["ten"] = 10  | 
 | 245 | +	numbers["three"] = 3  | 
 | 246 | + | 
 | 247 | +	// Initialize a map  | 
 | 248 | +	rating := map[string]float32{"C": 5, "Go": 4.5, "Python": 4.5, "C++": 2}  | 
 | 249 | + | 
 | 250 | +	fmt.Println("map numbers =", numbers)  | 
 | 251 | +	fmt.Println("The third number is: ", numbers["three"]) // get values  | 
 | 252 | +	// It prints: The third number is: 3  | 
 | 253 | + | 
 | 254 | +	// map has two return values. For second value, if the key doesn't exist,ok is false,true otherwise.  | 
 | 255 | +	csharpRating, ok := rating["C#"]  | 
 | 256 | +	if ok {  | 
 | 257 | +		fmt.Println("C# is in the map and its rating is ", csharpRating)  | 
 | 258 | +	} else {  | 
 | 259 | +		fmt.Println("We have no rating associated with C# in the map")  | 
 | 260 | +	}  | 
 | 261 | + | 
 | 262 | +	delete(rating, "C") // delete element with key "c"  | 
 | 263 | +	fmt.Printf("map rating = %#v\n", rating)  | 
 | 264 | +}  | 
 | 265 | +func main() {  | 
 | 266 | +	show_multiple_assignments()  | 
 | 267 | +	show_bool()  | 
 | 268 | +	show_different_types()  | 
 | 269 | +	show_strings()  | 
 | 270 | +	show_string_manipulation()  | 
 | 271 | +	show_errors()  | 
 | 272 | +	show_iota()  | 
 | 273 | +	set_default_values()  | 
 | 274 | +	show_arrays()  | 
 | 275 | +	show_slices()  | 
 | 276 | +	show_map()  | 
 | 277 | +}  | 
0 commit comments