_____________ SECTION1 Irene Zhang _____________ Table of Contents _________________ 1 Logging & Formatting 2 Declaration & Initialization 3 Functions 4 Memory 5 Sidebar: Go Unit testing 6 Arrays & Maps 7 Structs & Attached Functions 8 Control structures .. 8.1 if statements .. 8.2 for loop .. 8.3 switch statement 9 Useful things 1 Logging & Formatting ====================== 1. Use fmt.Println to print a line to stdout or log.Print for the labs to stderr 2. Set up and use gofmt 3. Go automatically inserts semicolons after any newline followed by any identifier, literal or token. Semicolons only appear in for loops and if statements 4. IMPORTANT! { cannot be on new line - Caps vs lower case: Determines whether externally visible or not! 2 Declaration & Initialization ============================== 1. var declares value 2. Assign in the usual way 3. := is short-hand for both declaration and definition 3 Functions =========== 1. Call a function in the usual way 2. Define a function, can have multiple return values 4 Memory ======== 1. Allocation: new= alloc and zeroes (8), make for slices, maps, and channels only 2. Go has a GC, so even things on stack you can use the pointers 3. And you can actually pass around those pointers 5 Sidebar: Go Unit testing ========================== - Used in all of the labs - test are in test_test.go - run them with 'go test' - run just one test with 'go test -run REGEX' 6 Arrays & Maps =============== 1. can declare array various ways 2. access array in the usual way 3. declare a map 4. allocate the map with make 5. add things to the map in the usual way 6. access the map in the usual way 7. ok, blank syntax 7 Structs & Attached Functions ============================== 1. structs are defined with variable name, then type. Remember to capitalize your variable names in the labs. 2. define an instance of your struct 3. new allocates and sets to 0 and returns pointer. Useful if init values are 0. 4. otherwise use {} with variable names 5. or without, then just set values in order 6. & equivalent to s := new(Student) with initial values Note: var s Student is local variable, new(Student) allocates and zeros, &Student{} is the same as new(Student) 7. structs can have attached functions. (s *Student) binds the function to the struct Student, functions can be attached to any named type 8 Control structures ==================== - accept initialization statements after condition 8.1 if statements ~~~~~~~~~~~~~~~~~ 8.2 for loop ~~~~~~~~~~~~ - no while or do while - range keyword 8.3 switch statement ~~~~~~~~~~~~~~~~~~~~ - really useful, can be used for anything - no break needed, just first match 9 Useful things =============== - defer