Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
0 who conceived go Robert Griesemer<div>Rob Pike</div><div>Ken Thompson</div>
0 CSP Communicating Sequential Processes<div><br></div><div>Tony Hoare 1978 paper on foundations of concurrency</div><div><br></div><div>Go ancestor</div>
0&nbsp;pernicious <div><div><div>having a harmful effect, especially in a gradual or subtle way<br></div></div></div>
p4 half-open intervals indexing intervals that include the first index but exclude the last<div><br></div><div>used in Go and most languages</div>
p4 slice interval omission: someslice[1:] if n is omitted, defaults to 0 or len(someslice)
p5 variable implicit initialization "if variable declaration doesn't initialize a value, the variable is implicitly initialized to the zero value for its type.<div><br></div><div>"""" for strings</div><div>0 for numbers</div>"
p9 allowed map key types a value of any type whose values can be compared with ==
&nbsp;p9 iteration order of map random.
p9 bufio.Scanner reads input and breaks it into lines or words, often easiest way to process input that comes naturally in lines
p10 fmt.Printf verbs first argument is a format string that specifies how subsequent arguments should be formatted<div><br></div><div><br></div>
p24 tagless switch "switch statement without an operand, equivalent to&nbsp;<center><table class=""highlighttable""><tbody><tr><td><div class=""linenodiv"" style=""background-color: #f0f0f0; padding-right: 10px""><pre style=""line-height: 125%"">1</pre></div></td><td class=""code""><div class=""highlight"" style=""background: #f8f8f8""><pre style=""line-height: 125%""><span style=""color: #008000; font-weight: bold"">switch</span> <span style=""color: #008000; font-weight: bold"">true</span> </pre></div> </td></tr></tbody></table></center><br>"
p28 predeclared names like <br>constants: true, false<div>types: int, float32</div><div>functions: make, len, append</div><div><br></div><div>* NOT reserved, may use them in declarations</div>
p28 four major kinds of declarations var<div>const</div><div>type</div><div>func</div>
p30 uninitialized variable There are no such things in go. Always initialized to at least zero-value of type.
p32 *int pronounciation pointer to int
p32 &amp;x pronounciation address of x
p34 new function "new(T) - creates unnamed variable of type T and returns it's address<br><br><center><table class=""highlighttable""><tbody><tr><td><div class=""linenodiv"" style=""background-color: #f0f0f0; padding-right: 10px""><pre style=""line-height: 125%"">1 2</pre></div></td><td class=""code""><div class=""highlight"" style=""background: #f8f8f8""><pre style=""line-height: 125%"">p <span style=""color: #666666"">:=</span> <span style=""color: #008000"">new</span>(<span style=""color: #B00040"">int</span>) fmt.Println(<span style=""color: #666666"">*</span>p) <span style=""color: #408080; font-style: italic"">// ""0""</span> </pre></div> </td></tr></tbody></table></center><br>"
"p35 exception to ""new returns distinct variable with unique address"" rule" two variables whose type carries no information and is therefore of size zero<div><br></div><div>struct{}</div><div>[0]int</div><div><br></div><div>These may have the same address</div>
p35 how garbage collector knows variable storage can be reclaimed? gist is:<div><br></div><div>every package-level var or every local variable of each currently active function can potentially be the start or root of a path to the variable in question. If no path exists, variable is unreachable.</div>
p36 x escapes from f "When local function variable ""x"" of function ""f"" is assigned to a global variable, and thus must be added to heap/stack"
p37 tuple assignment form of assignment that allows several variables to assigned at once<div><br></div><div>useful when swaping values of two variables</div>
p39 type declaration defines... "a new ""named type"" that has the same ""underlying type"" as an existing type<div><br></div><div>type name underlying-type</div><div><br></div><div>provides a way to separate different and perhaps incompatible uses of the underlying type so they can't be mixed unintentionally</div>"
p40 corresponding conversion operation for every type T, there is a corresponding conversion operation T(x)<div><br></div><div>a conversion from one type to another is allowed if both have the same underlying type, or if both are unnamed pointer types that point to variables of the same underyling type</div>
p44 package-level variable initialization dependencies variables initialized in order in which they are declared but dependencies are resolved first<div><br></div><div>var a = b + c // init 3rd</div><div>var b = f() // init second</div><div>var c = 1 // initialized first</div>
p44 package multiple .go files init order initialized in the order in which the files are given to the compiler, the go tool sorts the .go files by name before invoking the compiler<div><br></div><div><br></div>
p44 package initialization order one package at a time initialized, bottom up<div><br></div><div>main is last to be initialized</div>
p51 four catagories of types "basic types - numbers, strings, booleans<div><br></div><div>aggregate types - arrays, structs</div><div><br></div><div>reference types - pointers, slices, maps, functions, channels (they refer to program variables or state ""indirectly"")</div><div><br></div><div>interface types</div>"
p52 type rune synonym for int32<div><br></div><div>conventionally indicates that a value is a Unicode code point</div>
p52 type byte synonym for int8<div><br></div><div>emphasizes value is a piece of raw data</div>
p52 type uintptr width not specified, sufficient to hold all the bits of a pointer value
"p56 fmt.Printf ""adverbs"" # and [1]" "o := 0666<div>fmt.Printf(""%d %[1]o %#[1]o\n"", o) // ""438 666 0666""</div><div><br></div><div>[1] use first operand over and over</div><div># emit a 0 or 0x or 0X</div>"
p56 how to write rune literals "any character within single quotes<div><br></div><div><center><table class=""highlighttable""><tbody><tr><td><div class=""linenodiv"" style=""background-color: #f0f0f0; padding-right: 10px""><pre style=""line-height: 125%"">1</pre></div></td><td class=""code""><div class=""highlight"" style=""background: #f8f8f8""><pre style=""line-height: 125%"">somerune <span style=""color: #666666"">:=</span> '好' </pre></div> </td></tr></tbody></table></center><br></div>"
p64 len() function returns what when supplied a string returns number of bytes (not runes, so a chinese character would be 3 bytes)
p67&nbsp;parochial having a limited or narrow outlook or scope.
p70 unicode replacement character each time a UTF-8 decoder consumes an unexpected input byte, it generates a special Unicode replacement character '\uFFFD' (white question mark inside black diamond)
p71 why bytes.Buffer more efficient building up strings strings are immutable, building up strings incrementally can involve a lot of allocation and copying
p73 mutability difference strings vs byte slice string array of bytes is immutable<div><br></div><div>elements of a byte slice can be freely modified</div>
p74 usefulness of bytes.Buffer useful for efficient manipulation of byte slices<div><br></div><div>starts out empty but can grow as data is written to it</div>
p76 two other types of constants results of all arithmetic, logical and comparison operations applied to constant operands<div><br></div><div>results of conversions and calls to certain built-in functions such as len, cap, real, imag, complex, unsafe.Sizeof</div>
p77 iota "constant generator<div><br></div><div>used to create a sequence of related values without spelling out each explicitly</div><div><br></div><div><center><table class=""highlighttable""><tbody><tr><td><div class=""linenodiv"" style=""background-color: #f0f0f0; padding-right: 10px""><pre style=""line-height: 125%"">1 2 3 4 5 6 7 8</pre></div></td><td class=""code""><div class=""highlight"" style=""background: #f8f8f8""><pre style=""line-height: 125%""><span style=""color: #008000; font-weight: bold"">type</span> Weekday <span style=""color: #B00040"">int</span> <span style=""color: #008000; font-weight: bold"">const</span> ( Sunday Weekday = <span style=""color: #008000; font-weight: bold"">iota</span> Monday Tuesday <span style=""color: #666666"">...</span> ) </pre></div> </td></tr></tbody></table></center><br></div>"
p81 fixed/dynamic sized composite types fixed - arrays, structs<div>dynamic - slices, maps</div>
p82 is an array's size part of its type? "yes.<div><br></div><div><center><table class=""highlighttable""><tbody><tr><td><div class=""linenodiv"" style=""background-color: #f0f0f0; padding-right: 10px""><pre style=""line-height: 125%"">1 2</pre></div></td><td class=""code""><div class=""highlight"" style=""background: #f8f8f8""><pre style=""line-height: 125%"">q <span style=""color: #666666"">:=</span> [<span style=""color: #666666"">3</span>]{<span style=""color: #666666"">1</span>, <span style=""color: #666666"">2</span>, <span style=""color: #666666"">3</span>} q = [<span style=""color: #666666"">1</span>]{<span style=""color: #666666"">1</span>} <span style=""color: #408080; font-style: italic"">// error</span> </pre></div> </td></tr></tbody></table></center><br></div>"
p83 comparing arrays if array's element type is comparable, then the array type is comparable too
p84 slice operator s[i:j]<div><br></div><div>returns a new slice</div><div><br></div><div>s can be an array, pointer to an array, or another slice</div>
p86 nuance of passing a slice to a function a slice contains a pointer an element of an array, so passing a copy creates a reference
p86 slice literal creation s := []int{1, 2, 3}<div><br></div><div>looks like an array literal, but the size is not given. Implicitly creates an array variable of the right size and yields a slice that points to it.</div><div><br></div><div>This would be an array:</div><div>a := [...]{1, 2, 3}</div>
p86 array v slice comparability difference arrays are directly comparible if of the same type and size<div><br></div><div>slices are not</div>
p91 variadic func appendInt(x []int, y []int {}<div><br></div><div>y is variadic</div>
p94 reason why can't take the address of a map element growing a map might cause rehashing of existing elements into new storage locations, potentially invalidating the address
p95 iteration order of a map random
p95 the zero value of a map nil
p96 how to test if key exists in map "value, ok := someMap[""somekey""]"
p96 can maps be compared? no.<div><br></div><div><br></div>
p101 does field order matter to struct identity? yes.<div><br></div><div>type Employee struct {</div><div>&nbsp; name string</div><div>&nbsp; age int</div><div>}</div><div><br></div><div><div>type Employee struct {</div><div>&nbsp; age int<br></div><div><div>&nbsp; name string</div></div><div>}</div></div><div><br></div><div>^ different</div>
p104 struct comparability if all fields of a struct are comparable, the struct is comparable. Can use == or !=<div><br></div><div>* comparable struct types can be used as keys in a map</div>
p113 template actions portions of a template file enclosed in {unknown field ...}
p114 action daysAgo built in action, uses time.Since
p121 repositories of packages designed by the go team for applications such as networking, internationalization, image manipulation, etc<div><br></div><div><br></div>
p126 bare return "in a function with named results, the operands of a return statement may be omitted<div><br></div><div><center><table class=""highlighttable""><tbody><tr><td><div class=""linenodiv"" style=""background-color: #f0f0f0; padding-right: 10px""><pre style=""line-height: 125%"">1 2 3 4 5</pre></div></td><td class=""code""><div class=""highlight"" style=""background: #f8f8f8""><pre style=""line-height: 125%""><span style=""color: #008000; font-weight: bold"">func</span> foo(x <span style=""color: #B00040"">string</span>) (height, width <span style=""color: #B00040"">int</span>, err <span style=""color: #B00040"">error</span>) { err <span style=""color: #666666"">:=</span> foobar() <span style=""color: #008000; font-weight: bold"">if</span> err <span style=""color: #666666"">!=</span> <span style=""color: #008000; font-weight: bold"">nil</span> { <span style=""color: #008000; font-weight: bold"">return</span> <span style=""color: #408080; font-style: italic"">// bare return</span> } </pre></div> </td></tr></tbody></table></center><br></div>"
p129 fmt.Errorf() formats an error message using fmt.Sprintf and returns a new error value (how to extend an error with info basically)
p131 io.EOF special case error, used when reading file.
p136 acyclic graph no path from a node leads back to itself
p137 recursion anonymous function requirement must first declare a variable, then assign anonymous function to that variable (so func literal in scope to call itself recursively)
p139 breadth-first traversal Breadth-first search is an algorithm for traversing or searching tree or graph data structures. It starts at the tree root, and explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level.
p142 variadic functions "<center><table class=""highlighttable""><tbody><tr><td><div class=""linenodiv"" style=""background-color: #f0f0f0; padding-right: 10px""><pre style=""line-height: 125%"">1 2 3 4</pre></div></td><td class=""code""><div class=""highlight"" style=""background: #f8f8f8""><pre style=""line-height: 125%""><span style=""color: #008000; font-weight: bold"">func</span> sum(vals <span style=""color: #666666"">...</span><span style=""color: #B00040"">int</span>) <span style=""color: #B00040"">int</span> { values <span style=""color: #666666"">:=</span> []<span style=""color: #B00040"">int</span>{<span style=""color: #666666"">1</span>, <span style=""color: #666666"">2</span>, <span style=""color: #666666"">3</span>} sum(values<span style=""color: #666666"">...</span>) </pre></div> </td></tr></tbody></table></center><br>"
p145 multiple defer execution order the reverse order in which they were deferred
p147 can defer see and modify function return values? yes
p152 recover() can be called within deferred function, if function that called defer is panicking, recover can end the panic and cause the calling function to return normally
p156 method receiver "type Point struct{ X, Y float64 }<div>func (p Point) Distance(q Point) float64 {...} &lt;-- p is the ""receiver""</div>"
p158 named pointer types and methods named pointer types are not allowed to have method declarations
p158 name of this method: func (p *Point) ScaleBy(factor float64) {...} (*Point).ScaleBy<div><br></div><div>parens are necessary</div>
p161 embedded struct promotion "methods and properties are ""promoted"" to the struct where they're embedded"
p164 method value selector p.Distance (example) yields a method value, a function that binds a method to a specific receiver (p)<div><br></div><div>Can be invoked later without specific receiver value</div>
p164 method expression calling a method and providing a specific receiver<div><br></div><div>example:</div><div>distance := Point.Distance</div><div><br></div><div>distance(p, q) &lt;-- first argument is receiver</div>
p168 encapsulation variable or method is encapsulated if inaccessible to clients of the object
p183 dynamic dispatch since can't know what dynamic type of an interface value will be at compile time, use dynamic dispatch<div><br></div><div>compiler generates code to obtain the address of the method from type descriptor and makes indirect call to that address</div>
p182 interface dynamic _ and _ type and value
p184 comparison of interface values may be compared using == and !=<div><br></div><div>equal if&nbsp;</div><div>- both are nil</div><div>- if their dynamic types are identical and their dynamic values are equal according to the usual behavior of == for that type</div>
p184 fmt %T verb reports the dynamic type of an interface value
p187 StringSlice type provided by sort package that implements sort.interface on top of strings for sorting
p195 http.DefaultServeMux "convenience<div><br></div><div>to use as server's main handler, no need to pass it to ListenAndServe, just pass nil</div><div><br></div><div><center><table class=""highlighttable""><tbody><tr><td><div class=""linenodiv"" style=""background-color: #f0f0f0; padding-right: 10px""><pre style=""line-height: 125%"">1</pre></div></td><td class=""code""><div class=""highlight"" style=""background: #f8f8f8""><pre style=""line-height: 125%"">http.ListenAndServe(<span style=""color: #BA2121"">""""</span>, <span style=""color: #008000; font-weight: bold"">nil</span>) </pre></div> </td></tr></tbody></table></center><br></div>"
p196 fmt.Errorf wraps a call to errors.New()
p209 interface type assertion syntax sw, ok := w.(stringWriter)<br><br>stringWriter is defined having a method WriteString, the above code tests if w can satisfy
p211 discriminated unions interface use style exploits interface ability to hold values of a variety of concrete types and considers the interface to be the union of these types.
p212 type switch statement variation of switch statement for type assertions<div><br></div><div>switch x.(T) {</div><div>case nil: ...</div><div>case int, uint: ...</div><div>}</div>
p218 main goroutine the only goroutine that exists when a go program starts, invokes the main() function
p226 send operation unbuffered channel behavior blocks the sending goroutine until another goroutine executes a corresponding receive on the same channel
p230 unidirectional channels expose only either send or receive operations<div><br></div><div>chan&lt;-int (send only)</div><div>&lt;-chan int (receive only)</div>
p235 embarrassingly parallel problems that consist of subproblems that are completely independent of each other<div><br></div><div>enjoy performance that scales linearly with the amount of parallelism</div>
p244 select statement like a switch statement but for channels
p251 channel broadcast mechanism for reliably canceling n goroutines, we can exploit the feature that after a channel is closed and drained, subsequent receives proceed immediately yielding 0 values<div><br></div><div>close a channel to broadcast a cancelation event&nbsp;</div>
p260 data race occurs whenever two goroutines access the same variable concurrently and at least one of the accesses is a write
p261 memory go mantra """Do not communicate by sharing memory; instead, share memory by communicating"""
p261 monitor goroutine a goroutine that brokers access to a confined variable&nbsp;
p261 serial confinement when multiple goroutines in a pipeline share a variable but only access it in one goroutine (sequentially) at a time
p262 mutual exclusion allowing multiple goroutines to access a variable, but only one at a time
p263 critical section region of code between Lock and Unlock (sync.Mutex) in a goroutine where it is safe to access and modify shared variables
p264 monitor arrangement of functions that limit access to shared variables, mutex lock, and variables
p265 re-entrant locking a lock that's already locked<div><br></div><div>go's mutex locks ARE NOT re-entrant to avoid invariant inconsistencies&nbsp;</div>
p266 sync.RWMutex multiple readers, single writer lock<div><br></div><div>allows multiple readers to parallel access a variable if no writers are writing</div>
p270 sync.Once specialized solution to the problem of one time initialization<div><br></div><div><br></div>
p281 m:n scheduling go runtime contains its own scheduler that uses m:n scheduling to multiplex m goroutines on n threads<div><br></div><div>GOMAXPROCS is n</div>
p315 export_test.go file typical purpose "file that contains declarations for white-box tests to access privilaged members of a package under test ""back door"""
p329 reflection mechanism provided by go to inspect variables at runtime without knowing their types
p330 reflect.Type interface defined in reflect package, has methods for discriminating amongst types and inspecting their components
p330 reflect.TypeOf function accepts any interface{} returns its dynamic type as a reflect.Type
p331 fmt.Printf %T verb convenience verb that uses reflect.TypeOf to print the dynamic type of an interface value
p331 fmt.Println %v verb convenience verb that uses reflect.Value
p331 reflect.Value.Interface inverse operation to reflect.ValueOf() -- returns an interface{} holding the concrete value
p332 reflect.Value.Kind returns the finite number of kinds of type (one of) (basic, aggregate, reference, interface)
p342 reflect.Value.CanAddr "returns true/false if value is addressable (is a variable)<div><br></div><div>""we obtain an addressable reflect.Value whenever we indirect through a pointer""</div>"
p361 FFI foreign function interfaces<div><br></div><div>tools that create Go bindings for C functions</div>
"p363 purpose of (import ""C"")" "there is no package C<div><br></div><div>import ""C""</div><div>declaration causes go build to preprocess the file using cgo before the Go compiler sees it</div>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.