From 146bd9cce2d3d89fac28f4f6334d9b2acbed7dee Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Tue, 22 Aug 2023 05:52:14 -0700 Subject: [PATCH] Add examples of maps/slices packages --- examples/maps/maps.go | 12 +++++++++++- examples/maps/maps.hash | 4 ++-- examples/maps/maps.sh | 1 + examples/slices/slices.go | 12 +++++++++++- examples/slices/slices.hash | 4 ++-- examples/slices/slices.sh | 1 + public/maps | 31 +++++++++++++++++++++++++------ public/slices | 25 ++++++++++++++++++++++--- 8 files changed, 75 insertions(+), 15 deletions(-) diff --git a/examples/maps/maps.go b/examples/maps/maps.go index 79db9013c..40f7c5873 100644 --- a/examples/maps/maps.go +++ b/examples/maps/maps.go @@ -3,7 +3,10 @@ package main -import "fmt" +import ( + "fmt" + "maps" +) func main() { @@ -58,4 +61,11 @@ func main() { // the same line with this syntax. n := map[string]int{"foo": 1, "bar": 2} fmt.Println("map:", n) + + // The `maps` package contains a number of useful + // utility functions for maps. + n2 := map[string]int{"foo": 1, "bar": 2} + if maps.Equal(n, n2) { + fmt.Println("n == n2") + } } diff --git a/examples/maps/maps.hash b/examples/maps/maps.hash index d6abb5d57..32f844afe 100644 --- a/examples/maps/maps.hash +++ b/examples/maps/maps.hash @@ -1,2 +1,2 @@ -b976324013ef31370d1605d1834c5dd6f0db0ea1 -6gcPOX9Mm1R +c8435b8cc754213b70c58c9a51dfa824c6f70dd7 +5jpkxJ2T0Lv diff --git a/examples/maps/maps.sh b/examples/maps/maps.sh index 2b24dbb89..f234e9f60 100644 --- a/examples/maps/maps.sh +++ b/examples/maps/maps.sh @@ -9,3 +9,4 @@ map: map[k1:7] map: map[] prs: false map: map[bar:2 foo:1] +n == n2 diff --git a/examples/slices/slices.go b/examples/slices/slices.go index ac97a6b0f..541b582b3 100644 --- a/examples/slices/slices.go +++ b/examples/slices/slices.go @@ -3,7 +3,10 @@ package main -import "fmt" +import ( + "fmt" + "slices" +) func main() { @@ -70,6 +73,13 @@ func main() { t := []string{"g", "h", "i"} fmt.Println("dcl:", t) + // The `slices` package contains a number of useful + // utility functions for slices. + t2 := []string{"g", "h", "i"} + if slices.Equal(t, t2) { + fmt.Println("t == t2") + } + // Slices can be composed into multi-dimensional data // structures. The length of the inner slices can // vary, unlike with multi-dimensional arrays. diff --git a/examples/slices/slices.hash b/examples/slices/slices.hash index eb3c4ced7..e671db628 100644 --- a/examples/slices/slices.hash +++ b/examples/slices/slices.hash @@ -1,2 +1,2 @@ -9f1302a9c3cf79e5144c4818ea09465ff8d6da57 -553_cYPVlPH +522c14373ae9581dd3001be32530cdf940637646 +kiy1StxorBF diff --git a/examples/slices/slices.sh b/examples/slices/slices.sh index 8f5d1d4aa..42a400514 100644 --- a/examples/slices/slices.sh +++ b/examples/slices/slices.sh @@ -12,6 +12,7 @@ sl1: [c d e] sl2: [a b c d e] sl3: [c d e f] dcl: [g h i] +t == t2 2d: [[0] [1 2] [2 3 4]] # Check out this [great blog post](https://go.dev/blog/slices-intro) diff --git a/public/maps b/public/maps index 94427d5f3..1f2146e3c 100644 --- a/public/maps +++ b/public/maps @@ -42,7 +42,7 @@ - +
package main
@@ -53,7 +53,10 @@ -
import "fmt"
+
import (
+    "fmt"
+    "maps"
+)
@@ -192,10 +195,25 @@ itself, so we ignored it with the blank identifier the same line with this syntax.

- +
    n := map[string]int{"foo": 1, "bar": 2}
-    fmt.Println("map:", n)
+    fmt.Println("map:", n)
+ + + + + +

The maps package contains a number of useful +utility functions for maps.

+ + + + +
    n2 := map[string]int{"foo": 1, "bar": 2}
+    if maps.Equal(n, n2) {
+        fmt.Println("n == n2")
+    }
 }
@@ -220,7 +238,8 @@ printed with fmt.Println.

map: map[k1:7] map: map[] prs: false -map: map[bar:2 foo:1] +map: map[bar:2 foo:1] +n == n2 @@ -239,7 +258,7 @@ printed with fmt.Println.

diff --git a/public/slices b/public/slices index f26101e7c..dc8889d79 100644 --- a/public/slices +++ b/public/slices @@ -42,7 +42,7 @@ a more powerful interface to sequences than arrays.

- +
package main
@@ -53,7 +53,10 @@ a more powerful interface to sequences than arrays.

-
import "fmt"
+
import (
+    "fmt"
+    "slices"
+)
@@ -210,6 +213,21 @@ in a single line as well.

+ + +

The slices package contains a number of useful +utility functions for slices.

+ + + + +
    t2 := []string{"g", "h", "i"}
+    if slices.Equal(t, t2) {
+        fmt.Println("t == t2")
+    }
+ + +

Slices can be composed into multi-dimensional data @@ -256,6 +274,7 @@ they are rendered similarly by fmt.Println.

sl2: [a b c d e] sl3: [c d e f] dcl: [g h i] +t == t2 2d: [[0] [1 2] [2 3 4]] @@ -300,7 +319,7 @@ Go’s other key builtin data structure: maps.