From 427f87c65588374352c6a85e52758a9609baa9bc Mon Sep 17 00:00:00 2001 From: Carl Date: Sat, 4 Jun 2016 00:26:18 -0700 Subject: Updated closure Corrected closure example that referenced assigning x to e^10, which does not happen in this iteration. Set x to a value large enough to make the comments hold true. --- go.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'go.html.markdown') diff --git a/go.html.markdown b/go.html.markdown index dc684227..7b007bab 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -221,7 +221,8 @@ func learnFlowControl() { xBig := func() bool { return x > 10000 // References x declared above switch statement. } - fmt.Println("xBig:", xBig()) // true (we last assigned e^10 to x). + x = 99999 + fmt.Println("xBig:", xBig()) // true x = 1.3e3 // This makes x == 1300 fmt.Println("xBig:", xBig()) // false now. -- cgit v1.2.3 From 5f9d8fe4e0f8f70855708cec9ac6713feb46eaa9 Mon Sep 17 00:00:00 2001 From: Leonid Shevtsov Date: Mon, 31 Oct 2016 19:08:32 +0200 Subject: [go] added practical examples for the underscore (#2414) * go: added practical examples for the underscore * Example of using underscore to discard the error * Example of using underscore to loop over values of a slice * Incidentally, example of writing to a file * go: Adjust justification for ignoring error value --- go.html.markdown | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'go.html.markdown') diff --git a/go.html.markdown b/go.html.markdown index 7b007bab..f097caeb 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -11,6 +11,7 @@ contributors: - ["Jose Donizetti", "https://github.com/josedonizetti"] - ["Alexej Friesen", "https://github.com/heyalexej"] - ["Clayton Walker", "https://github.com/cwalk"] + - ["Leonid Shevtsov", "https://github.com/leonid-shevtsov"] --- Go was created out of the need to get work done. It's not the latest trend @@ -39,6 +40,7 @@ import ( "io/ioutil" // Implements some I/O utility functions. m "math" // Math library with local alias m. "net/http" // Yes, a web server! + "os" // OS functions like working with the file system "strconv" // String conversions. ) @@ -133,6 +135,14 @@ can include line breaks.` // Same string type. // Unused variables are an error in Go. // The underscore lets you "use" a variable but discard its value. _, _, _, _, _, _, _, _, _, _ = str, s2, g, f, u, pi, n, a3, s4, bs + // Usually you use it to ignore one of the return values of a function + // For example, in a quick and dirty script you might ignore the + // error value returned from os.Create, and expect that the file + // will always be created. + file, _ := os.Create("output.txt") + fmt.Fprint(file, "This is how you write to a file, by the way") + file.Close() + // Output of course counts as using a variable. fmt.Println(s, c, a4, s3, d2, m) @@ -211,6 +221,10 @@ func learnFlowControl() { // for each pair in the map, print key and value fmt.Printf("key=%s, value=%d\n", key, value) } + // If you only need the value, use the underscore as the key + for _, name := range []string{"Bob", "Bill", "Joe"} { + fmt.Printf("Hello, %s\n", name) + } // As with for, := in an if statement means to declare and assign // y first, then test y > x. -- cgit v1.2.3 From 801484a23b3a65b7813929a390684b9f965729eb Mon Sep 17 00:00:00 2001 From: gondo Date: Sun, 2 Jul 2017 20:38:55 +0800 Subject: more clear explanation for interface (#2779) more clear explanation why interface is consider defined without specifically declaring it (like in other languages `pair implement Stringer`) --- go.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'go.html.markdown') diff --git a/go.html.markdown b/go.html.markdown index f097caeb..1e121b0f 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -295,7 +295,7 @@ type pair struct { x, y int } -// Define a method on type pair. Pair now implements Stringer. +// Define a method on type pair. Pair now implements Stringer because Pair has defined all the methods in the interface. func (p pair) String() string { // p is called the "receiver" // Sprintf is another public function in package fmt. // Dot syntax references fields of p. -- cgit v1.2.3 From 88e1938994d0c55a03851e8c561dc0c30b745401 Mon Sep 17 00:00:00 2001 From: ven Date: Tue, 4 Jul 2017 13:34:50 +0200 Subject: fix #2700 --- go.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'go.html.markdown') diff --git a/go.html.markdown b/go.html.markdown index 1e121b0f..50692f9c 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -99,12 +99,12 @@ can include line breaks.` // Same string type. // Arrays have size fixed at compile time. var a4 [4]int // An array of 4 ints, initialized to all 0. - a3 := [...]int{3, 1, 5} // An array initialized with a fixed size of three - // elements, with values 3, 1, and 5. + a5 := [...]int{3, 1, 5, 10, 100} // An array initialized with a fixed size of fize + // elements, with values 3, 1, 5, 10, and 100. // Slices have dynamic size. Arrays and slices each have advantages // but use cases for slices are much more common. - s3 := []int{4, 5, 9} // Compare to a3. No ellipsis here. + s3 := []int{4, 5, 9} // Compare to a5. No ellipsis here. s4 := make([]int, 4) // Allocates slice of 4 ints, initialized to all 0. var d2 [][]float64 // Declaration only, nothing allocated here. bs := []byte("a slice") // Type conversion syntax. @@ -134,7 +134,7 @@ can include line breaks.` // Same string type. // Unused variables are an error in Go. // The underscore lets you "use" a variable but discard its value. - _, _, _, _, _, _, _, _, _, _ = str, s2, g, f, u, pi, n, a3, s4, bs + _, _, _, _, _, _, _, _, _, _ = str, s2, g, f, u, pi, n, a5, s4, bs // Usually you use it to ignore one of the return values of a function // For example, in a quick and dirty script you might ignore the // error value returned from os.Create, and expect that the file -- cgit v1.2.3