29 min watch
Go Code Roast: Secret Hitler Game
Watch as I review a simple game, written in Go, and explain what I like, what I don't like, and how the author could improve his chances of landing a job by showing off this code.
2 min read
Nesting composite literals
ReminderMonday I’ll be building a Go web server during my weekly live stream, following boot.dev’s “Learn Web Servers” course. I hope you can join me Composite literals … Within a composite literal of array, slice, or map type T, elements or map keys that are themselves composite literals may elide the respective literal type if it is identical to the element or key type of T. Similarly, elements or keys that are addresses of composite literals may elide the &T when the element or key type is *T.
2 min read
Slice literals
ReminderMonday I’ll be building a Go web server during my weekly live stream, following boot.dev’s “Learn Web Servers” course. I hope you can join me Composite literals … A slice literal describes the entire underlying array literal. Thus the length and capacity of a slice literal are the maximum element index plus one. A slice literal has the form []T{x1, x2, … xn} and is shorthand for a slice operation applied to an array:
2 min read
Quiz results
I recently asked the following quiz question on this list, as well as on social media. What does the following code print? var hmm = [...]int{1, 2, 3, 12: 9, 8: 3} fmt.Println(len(hmm)) On social media, I provided the following options: 5 7 13 Doesn’t compile And now the results are in! I got a total of 428 responses across LinkedIn, Twitter, and Mastodon: The good news is: The majority are right!
1 min read
Special considerations for array literals
It’s Monday again. That means I’ll be live-coding again! I hope you can join me. Composite literals … The length of an array literal is the length specified in the literal type. If fewer elements than the length are provided in the literal, the missing elements are set to the zero value for the array element type. It is an error to provide elements with index values outside the index range of the array.
1 min read
I'm going back to school
Watch me learn to build a web server in Go, courtesy of boot.dev.
2 min read
Zero values of slices and maps
Composite literals … Note that the zero value for a slice or map type is not the same as an initialized but empty value of the same type. Let’s look at examples so we can understand exactly what is being stated here. Recall that when you declare a variable of a given type, the default value is the zero value for the type: var s1 []string // Zero value for slice var m1 map[string]int // Zero value for map What the spec is telling us is that the zero value of a slice or map is not the same as an initialized, but empty, slice or map:
Subscribe to Boldly Go: Daily
Every day I'll send you advice to improve your understanding of Go. Don't miss out! I will respect your inbox, and honor my privacy policy.
Unsure? Browse the archive.
1 min read
Taking the address of literals
Composite literals … Taking the address of a composite literal generates a pointer to a unique variable initialized with the literal’s value. var pointer *Point3D = &Point3D{y: 1000} This particular feature of the language is immensely useful. Without it, the above code snippet would become much longer: var point Point3D = Point3D{y: 1000} var pointer *Point3D = &point In fact, this is the exact situation we are in for non-composites.
1 min read
Array and slice literals
Composite literals … For array and slice literals the following rules apply: Each element has an associated integer index marking its position in the array. An element with a key uses the key as its index. The key must be a non-negative constant representable by a value of type int; and if it is typed it must be of integer type. An element without a key uses the previous element’s index plus one.
2 min read
Struct literals
Composite literals … For struct literals the following rules apply: A key must be a field name declared in the struct type. Probably self-evident. You can’t use field names that aren’t defined in the struct type. An element list that does not contain any keys must list an element for each struct field in the order in which the fields are declared. If any element has a key, every element must have a key.
1 min read
Duplicate map keys
It’s Monday again. That means I’ll be live-coding again! I hope you can join me. Composite literals … For map literals, all elements must have a key. It is an error to specify multiple elements with the same field name or constant key value. For non-constant map keys, see the section on evaluation order. I expect we can all agree this makes sense. Keys in a map literal must be unique.