exec
+// exec
// function.
package main
-import "syscall"
-import "os"
-import "os/exec"
+import (
+ "os"
+ "os/exec"
+ "syscall"
+)
func main() {
@@ -26,7 +28,7 @@ func main() {
}
// `Exec` requires arguments in slice form (as
- // apposed to one big string). We'll give `ls` a few
+ // opposed to one big string). We'll give `ls` a few
// common arguments. Note that the first argument should
// be the program name.
args := []string{"ls", "-a", "-l", "-h"}
diff --git a/examples/execing-processes/execing-processes.hash b/examples/execing-processes/execing-processes.hash
index 4701404c6..90bd32578 100644
--- a/examples/execing-processes/execing-processes.hash
+++ b/examples/execing-processes/execing-processes.hash
@@ -1,2 +1,2 @@
-b527bbb76a42dd4bae541b73a7377b7e83e79905
-bf11ADw-2Ho
+568ae983493addff02d2ce8df57f41daf537f077
+s9qg7olf1dM
diff --git a/examples/exit/exit.go b/examples/exit/exit.go
index d1e3646fa..578e553e7 100644
--- a/examples/exit/exit.go
+++ b/examples/exit/exit.go
@@ -3,8 +3,10 @@
package main
-import "fmt"
-import "os"
+import (
+ "fmt"
+ "os"
+)
func main() {
diff --git a/examples/exit/exit.hash b/examples/exit/exit.hash
index 988b8f323..6be0f6336 100644
--- a/examples/exit/exit.hash
+++ b/examples/exit/exit.hash
@@ -1,2 +1,2 @@
-dc0bb3eaafa045d6aa05e88aff39322a1ccf822e
-vDaM0-MGJ_k
+16f2c50f58d9d113f2cdd5367ddd95a220d89b19
+b9aYzlENkb__R
diff --git a/examples/file-paths/file-paths.hash b/examples/file-paths/file-paths.hash
index f1610a14c..872cc7b50 100644
--- a/examples/file-paths/file-paths.hash
+++ b/examples/file-paths/file-paths.hash
@@ -1,2 +1,2 @@
-1215302b9e59ee9dee21dcd3c47d5f6c672fb058
-QIitbMNiFRx
+10823f6a3f4daea097a91374efa88c4361932488
+5h3lUytvmyO
diff --git a/examples/for/for.go b/examples/for/for.go
index 7c4aa04f7..83ca1bf71 100644
--- a/examples/for/for.go
+++ b/examples/for/for.go
@@ -1,5 +1,5 @@
// `for` is Go's only looping construct. Here are
-// three basic types of `for` loops.
+// some basic types of `for` loops.
package main
@@ -15,10 +15,16 @@ func main() {
}
// A classic initial/condition/after `for` loop.
- for j := 7; j <= 9; j++ {
+ for j := 0; j < 3; j++ {
fmt.Println(j)
}
+ // Another way of accomplishing the basic "do this
+ // N times" iteration is `range` over an integer.
+ for i := range 3 {
+ fmt.Println("range", i)
+ }
+
// `for` without a condition will loop repeatedly
// until you `break` out of the loop or `return` from
// the enclosing function.
@@ -29,7 +35,7 @@ func main() {
// You can also `continue` to the next iteration of
// the loop.
- for n := 0; n <= 5; n++ {
+ for n := range 6 {
if n%2 == 0 {
continue
}
diff --git a/examples/for/for.hash b/examples/for/for.hash
index 7852dec97..0beaf5928 100644
--- a/examples/for/for.hash
+++ b/examples/for/for.hash
@@ -1,2 +1,2 @@
-33056d6b36f9894fb6359c9cf2ef8725bbdafa19
-lGYfUJwiGfi
+8eeb5be15c3c5fc3f9d0d8009dfcec771dc5e03d
+_F2rYHNilKa
diff --git a/examples/for/for.sh b/examples/for/for.sh
index 12785ebb4..b7bc0767b 100644
--- a/examples/for/for.sh
+++ b/examples/for/for.sh
@@ -2,9 +2,12 @@ $ go run for.go
1
2
3
-7
-8
-9
+0
+1
+2
+range 0
+range 1
+range 2
loop
1
3
diff --git a/examples/functions/functions.hash b/examples/functions/functions.hash
index 85d4e8fe2..c1efad973 100644
--- a/examples/functions/functions.hash
+++ b/examples/functions/functions.hash
@@ -1,2 +1,2 @@
-ae669923c20e5ebf4a7b4b11b8fdf2972accf9e2
-hzGUvK6iJNm
+94ade6d23721234a9612c9f77431106308b84953
+-o49-dQfGbK
diff --git a/examples/generics/generics.go b/examples/generics/generics.go
new file mode 100644
index 000000000..470594293
--- /dev/null
+++ b/examples/generics/generics.go
@@ -0,0 +1,79 @@
+// Starting with version 1.18, Go has added support for
+// _generics_, also known as _type parameters_.
+
+package main
+
+import "fmt"
+
+// As an example of a generic function, `SlicesIndex` takes
+// a slice of any `comparable` type and an element of that
+// type and returns the index of the first occurrence of
+// v in s, or -1 if not present. The `comparable` constraint
+// means that we can compare values of this type with the
+// `==` and `!=` operators. For a more thorough explanation
+// of this type signature, see [this blog post](https://go.dev/blog/deconstructing-type-parameters).
+// Note that this function exists in the standard library
+// as [slices.Index](https://pkg.go.dev/slices#Index).
+func SlicesIndex[S ~[]E, E comparable](s S, v E) int {
+ for i := range s {
+ if v == s[i] {
+ return i
+ }
+ }
+ return -1
+}
+
+// As an example of a generic type, `List` is a
+// singly-linked list with values of any type.
+type List[T any] struct {
+ head, tail *element[T]
+}
+
+type element[T any] struct {
+ next *element[T]
+ val T
+}
+
+// We can define methods on generic types just like we
+// do on regular types, but we have to keep the type
+// parameters in place. The type is `List[T]`, not `List`.
+func (lst *List[T]) Push(v T) {
+ if lst.tail == nil {
+ lst.head = &element[T]{val: v}
+ lst.tail = lst.head
+ } else {
+ lst.tail.next = &element[T]{val: v}
+ lst.tail = lst.tail.next
+ }
+}
+
+// AllElements returns all the List elements as a slice.
+// In the next example we'll see a more idiomatic way
+// of iterating over all elements of custom types.
+func (lst *List[T]) AllElements() []T {
+ var elems []T
+ for e := lst.head; e != nil; e = e.next {
+ elems = append(elems, e.val)
+ }
+ return elems
+}
+
+func main() {
+ var s = []string{"foo", "bar", "zoo"}
+
+ // When invoking generic functions, we can often rely
+ // on _type inference_. Note that we don't have to
+ // specify the types for `S` and `E` when
+ // calling `SlicesIndex` - the compiler infers them
+ // automatically.
+ fmt.Println("index of zoo:", SlicesIndex(s, "zoo"))
+
+ // ... though we could also specify them explicitly.
+ _ = SlicesIndex[[]string, string](s, "zoo")
+
+ lst := List[int]{}
+ lst.Push(10)
+ lst.Push(13)
+ lst.Push(23)
+ fmt.Println("list:", lst.AllElements())
+}
diff --git a/examples/generics/generics.hash b/examples/generics/generics.hash
new file mode 100644
index 000000000..ef92af1b8
--- /dev/null
+++ b/examples/generics/generics.hash
@@ -0,0 +1,2 @@
+1ad71763360077271687c5e9d147c89c0b580b0a
+7v7vElzhAeO
diff --git a/examples/generics/generics.sh b/examples/generics/generics.sh
new file mode 100644
index 000000000..90b52a3d5
--- /dev/null
+++ b/examples/generics/generics.sh
@@ -0,0 +1,3 @@
+$ go run generics.go
+index of zoo: 2
+list: [10 13 23]
diff --git a/examples/goroutines/goroutines.go b/examples/goroutines/goroutines.go
index c2bdde87a..2b9e26c1e 100644
--- a/examples/goroutines/goroutines.go
+++ b/examples/goroutines/goroutines.go
@@ -2,10 +2,13 @@
package main
-import "fmt"
+import (
+ "fmt"
+ "time"
+)
func f(from string) {
- for i := 0; i < 3; i++ {
+ for i := range 3 {
fmt.Println(from, ":", i)
}
}
@@ -29,9 +32,8 @@ func main() {
}("going")
// Our two function calls are running asynchronously in
- // separate goroutines now, so execution falls through
- // to here. This `Scanln` requires we press a key
- // before the program exits.
- fmt.Scanln()
+ // separate goroutines now. Wait for them to finish
+ // (for a more robust approach, use a [WaitGroup](waitgroups)).
+ time.Sleep(time.Second)
fmt.Println("done")
}
diff --git a/examples/goroutines/goroutines.hash b/examples/goroutines/goroutines.hash
index e86550b81..7ea80e0b6 100644
--- a/examples/goroutines/goroutines.hash
+++ b/examples/goroutines/goroutines.hash
@@ -1,2 +1,2 @@
-bfdaa0c8104c1257e6fea102fd26d476a3e8c14e
-6Y8t3Yxd1LD
+b7455068d7f944d7c1a2764e5ec05bee53296e62
+0fx_WokYVFO
diff --git a/examples/goroutines/goroutines.sh b/examples/goroutines/goroutines.sh
index 2dcf3bb37..38506edba 100644
--- a/examples/goroutines/goroutines.sh
+++ b/examples/goroutines/goroutines.sh
@@ -1,7 +1,8 @@
# When we run this program, we see the output of the
-# blocking call first, then the interleaved output of the
-# two goroutines. This interleaving reflects the
-# goroutines being run concurrently by the Go runtime.
+# blocking call first, then the output of the two
+# goroutines. The goroutines' output may be interleaved,
+# because goroutines are being run concurrently by the
+# Go runtime.
$ go run goroutines.go
direct : 0
direct : 1
@@ -10,7 +11,6 @@ goroutine : 0
going
goroutine : 1
goroutine : 2
-Sorry, we couldn't find that! Check out the home page?
- + + +