Appending to slices

Appending to and copying slices The built-in functions append and copy assist in common slice operations. For both functions, the result is independent of whether the memory referenced by the arguments overlaps. Today and tomorrow we’ll look at the first of those two, append: The variadic function append appends zero or more values x to a slice s and returns the resulting slice of the same type as s. The core type of s must be a slice of type []E.


Built-in functions

We’ve just finished up the rather long section on different types of statements. There’s only a few more sections in the spec, before we finish this series. And today we start looking at Built-in functions! Built-in functions Built-in functions are predeclared. They are called like any other function but some of them accept a type instead of an expression as the first argument. The built-in functions do not have standard Go types, so they can only appear in call expressions; they cannot be used as function values.


Modifying return values in deferred functions

Oops! It seems that Friday’s email got stuck, and didn’t actually send until late Monday. Meaning you probably got two emails in the same day, and out of order. I hope it didn’t bother anyone too much. I should probably make a joke about how it was deferred… Nah! Today we’ll be finishing up the section on the defer keyword, with one special capability that it affords us. Defer statements …


Defer execution order

Let’s talk about some of the nuance surrounding defer that’s often lost, or simply forgotten. Defer statements … Each time a “defer” statement executes, the function value and parameters to the call are evaluated as usual and saved anew but the actual function is not invoked. Instead, deferred functions are invoked immediately before the surrounding function returns, in the reverse order they were deferred. That is, if the surrounding function returns through an explicit return statement, deferred functions are executed after any result parameters are set by that return statement but before the function returns to its caller.

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.


Defer statements

We’re at the last item in the “Statements” section of the spec, before we move on to built-in functions. And naturally, the last one is… the defer statement~ Defer statements A “defer” statement invokes a function whose execution is deferred to the moment the surrounding function returns, either because the surrounding function executed a return statement, reached the end of its function body, or because the corresponding goroutine is panicking.


Fallthrough statements

Fallthrough statements A “fallthrough” statement transfers control to the first statement of the next case clause in an expression “switch” statement. It may be used only as the final non-empty statement in such a clause. FallthroughStmt = "fallthrough" . Well that’s pretty straight forward. for x := range 10 { switch { case x%2 == 0: fmt.Print("X") fallthrough case x%3 == 0: fmt.Print("x") fallthrough case x%5 == 0: fmt.Print("O") } fmt.


Goto statements

We’ve finished the discussion of the new range-over-func feature, so let’s continue where we left off before the Go 1.23 release, with a feature you’ll rarely, if ever, use. Goto statements A “goto” statement transfers control to the statement with the corresponding label within the same function. GotoStmt = "goto" Label . goto Error Unless your only programming experience has been in the BASIC language, you’ve probably learned to eschew “goto”.


Tree walking with range-over-fun

We’ve been looking at Go 1.23’s new range-over-func feature. And we’re just about through what the spec has to say about it. Except for one thing. The example code! // iteration support for a recursive tree data structure type Tree[K cmp.Ordered, V any] struct { left, right *Tree[K, V] key K value V } func (t *Tree[K, V]) walk(yield func(key K, val V) bool) bool { return t == nil || t.


yield

For statements with range clause … For a function f, the iteration proceeds by calling f with a new, synthesized yield function as its argument. Let’s talk about yield today. If I’m honest, this was probably the scariest part of these new function iterators, before I started actually using them. It looks an awful lot like a new keyword, with weird usage. I know I’m not alone in thinking this.


Iterating over functions

As mentioned yesterday, today we’re taking a bit of a step back to an earlier portion of the spec, which was just updated. Here’s the first part of the updated section of the spec (new additions emphasized): For statements with range clause A “for” statement with a “range” clause iterates through all entries of an array, slice, string or map, values received on a channel, integer values from zero to an upper limit [Go 1.


Go 1.23.0 has been released!

I missed the last couple of days. Oops! It’s a busy time here, as I’m preparing for an international move, and balancing a new business venture I’m working on. But I’m back today! The big news in the Go world is that on Tuesday, Go 1.23.0 was released! What this means for us, as we venture through the Go spec, is that there are new changes to the spec we need to talk about!