Closures

Today I’m live streaming again at the regular time. Last week I missed, due to some hardware problems. But I’m back up and running! Join me as I go through boot.dev’s “Learn Web Servers” course that I had planned for last week. I hope you can join me Closures are a common concept in many langauges. If you’ve used them before, there’s nothing really new or interesting about the way Go handles them.


Function literals

This week’s episode of the Cup o’ Go podcast is out! Check out the latest Go news, and an interview with Tim Stiles about hacking 🧬 DNA with Go. We should all be familiar with functions, and how to create them in Go: func Foo() { /* ... */ } But we can also have function literals… Function literals A function literal represents an anonymous function. Function literals cannot declare type parameters.


(Bad) examples of valid composite literals

Today we round out the spec’s discussion of composite literals, with a few examples. Including some bad ones. 🤷 Composite literals … Examples of valid array, slice, and map literals: // list of prime numbers primes := []int{2, 3, 5, 7, 9, 2147483647} // vowels[ch] is true if ch is a vowel vowels := [128]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true, 'y': true} // the array [10]float32{-1, 0, 0, 0, -0.


Ambiguous composite literals

I had planned to live stream this afternoon, but ran into some hardware problems that left me scrambling to get my PC booting again. It is now booting, and barring any other unforseen complications, I’ll be doing the planned boot.dev’s “Learn Web Servers” course next week at the regular time. I hope you can join me Here’s one of the bits of the Go spec you probably never knew existed (I didn’t), and will never need to care about.

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.

Code Review

29 min watch


Go Code Roast: Secret Hitler Game

Watch as I review a simple game, writte 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.


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.


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:


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!


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.

How-Tos

1 min read


I'm going back to school

Watch me learn to build a web server in Go, courtesy of boot.dev.


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: