Type assertion values

By now you’ve guessed there was no live stream today. Illess has been hitting my family hard. I’ll try to pick it up again after the new year. Type assertions … If the type assertion holds, the value of the expression is the value stored in x and its type is T. If the type assertion is false, a run-time panic occurs. In other words, even though the dynamic type of x is known only at run time, the type of x.


Type assertions of interface types

Type assertions … If T is an interface type, x.(T) asserts that the dynamic type of x implements the interface T. This simple sentence has some pretty profound implications. Let’s start with a simple example: type Fooer interface { Foo() } type thing int func (thing) Foo() {} var x interface{} = thing(3) x.(Fooer) // Valid, the result is an interface of type Fooer This can be very useful to convert from the empty interface (aka interface{} or any) to a more specific interface, as in the above example.


Type assertions of non-interface types

Yesterday we started on type assertions. Today we’ll look at some of the specific details, as they relate to type assertions to non-interface types. Tomorrow we’ll look at interface types. Type assertions … More precisely, if T is not an interface type, x.(T) asserts that the dynamic type of x is identical to the type T. Recall that variables of interface types contain a dynamic type, which represents the type that satisfies the interface:


Type assertions

Type assertions For an expression x of interface type, but not a type parameter, and a type T, the primary expression x.(T) asserts that x is not nil and that the value stored in x is of type T. The notation x.(T) is called a type assertion. For now, let’s look at some examples of type assertions that hold, and that don’t. In the next day or two, we’ll look at more details from the spec.

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.


Half way there

No live stream today. I’m still feeling a bit under the weather. Hopefully next week we’ll be back on schedule. In January, I started this series on the Go spec, not having any idea if I'd have even 2 readers, or how long it would take to get through the whole spec. This week we passed the half-way mark on that journey through the spec, and there are over 400 of you subscribed to this daily list.


Full slice expressions

Full slice expressions The primary expression a[low : high : max] constructs a slice of the same type, and with the same length and elements as the simple slice expression a[low : high]. Additionally, it controls the resulting slice’s capacity by setting it to max - low. So I’ll be honest here and say I’ve never used this slice expression format. I’ve been vaguely aware that it exists, but I’ve never felt the need for it.


Slice expression result types

No live stream today. Next week I expect to be back at it! Forgive me for not sending an email Friday. I spent the day in bed. 🛏️ But I’m back on my feet now! So up to now we’ve learned how to use slice expressions… but what is the type that results? Well, you can probably guess. It’s pretty intuitive: Simple slice expressions … Except for untyped strings, if the sliced operand is a string or slice, the result of the slice operation is a non-constant value of the same type as the operand.


Range of slice expressions

Simple slice expressions … For arrays or strings, the indices are in range if 0 <= low <= high <= len(a), otherwise they are out of range. This is probably intuitive. a := [5]int{1, 2, 3, 4, 5} s := "Abracadabra" a[-1] // Out of range s[5:1] // Out of range a[12] // Out of range s[999] // Out of range For slices, the upper index bound is the slice capacity cap(a) rather than the length.

Quiz Challenge

1 min read


Quiz

Today’s quiz day. Tomorrow I’ll provide the answer as we continue with the section of the spec that explains today’s code. Consider the following program. What is its output? package main import "fmt" func main() { a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} s := a[2:5] s2 := s[5:7] fmt.Println(s2) } [6 7] [8 9] A runtime panic A compilation error


Simple slice expression shortcuts

Simple slice expressions, which we started with yesterday, offer us two shortcuts: Simple slice expressions … For convenience, any of the indices may be omitted. A missing low index defaults to zero; a missing high index defaults to the length of the sliced operand: a[2:] // same as a[2 : len(a)] a[:3] // same as a[0 : 3] a[:] // same as a[0 : len(a)] This should be pretty easy to understand.


Slice expressions

Last week’s livestream turned out to be a bit of a disappointment, as some Docker problems prevented me from making any real progress. I’ve resolved that, so today I hope to hit the ground running with some Kubernetes deployments! I hope you can join me again today on my live stream. Ever wonder why Go uses the terminology “slice”, rather than “array”, to refer to a variable list of things? Today’s section explains that.