From f63032d2f216d62d149a8dc2f1df919999c41938 Mon Sep 17 00:00:00 2001 From: Kev Choi Date: Mon, 20 Apr 2015 23:26:38 -0700 Subject: Add default case to switch statement in Go --- go.html.markdown | 2 ++ 1 file changed, 2 insertions(+) (limited to 'go.html.markdown') diff --git a/go.html.markdown b/go.html.markdown index 17f10bd9..9b9758b4 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -179,6 +179,8 @@ func learnFlowControl() { // Cases don't "fall through". case 43: // Unreached. + default: + // Default case is optional. } // Like if, for doesn't use parens either. // Variables declared in for and if are local to their scope. -- cgit v1.2.3 From 3bf74b3ddae006353069218563cbcd370c88aaaa Mon Sep 17 00:00:00 2001 From: Samuel Marks Date: Wed, 22 Apr 2015 23:54:16 +1000 Subject: Fixed grammar, added explanation of function signature and arguments --- go.html.markdown | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'go.html.markdown') diff --git a/go.html.markdown b/go.html.markdown index 9b9758b4..9fce7a9b 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -64,7 +64,11 @@ func beyondHello() { learnTypes() // < y minutes, learn more! } -// Functions can have parameters and (multiple!) return values. +/* <- multiline comment +Functions can have parameters and (multiple!) return values. +Here `x`, `y` are the arguments and `sum`, `prod` is the signature (what's returned). +Note that `x` and `sum` receive the type `int`. +*/ func learnMultiple(x, y int) (sum, prod int) { return x + y, x * y // Return two values. } @@ -83,7 +87,7 @@ can include line breaks.` // Same string type. f := 3.14195 // float64, an IEEE-754 64-bit floating point number. c := 3 + 4i // complex128, represented internally with two float64's. - // Var syntax with an initializers. + // var syntax with initializers. var u uint = 7 // Unsigned, but implementation dependent size as with int. var pi float32 = 22. / 7 -- cgit v1.2.3 From 1faab2901edb8c2e1eda708af8b37f3ec62dfd78 Mon Sep 17 00:00:00 2001 From: Samuel Marks Date: Thu, 23 Apr 2015 00:02:33 +1000 Subject: Fallthrough --- go.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'go.html.markdown') diff --git a/go.html.markdown b/go.html.markdown index 9fce7a9b..34b855e3 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -181,6 +181,10 @@ func learnFlowControl() { case 1: case 42: // Cases don't "fall through". + /* + There is a `fallthrough` keyword however, see: + https://github.com/golang/go/wiki/Switch#fall-through + */ case 43: // Unreached. default: -- cgit v1.2.3 From 6a2fe434b837cbc52895511138b86287d738bc46 Mon Sep 17 00:00:00 2001 From: Clayton Walker Date: Tue, 6 Oct 2015 23:56:55 -0400 Subject: Minor Typos, increased readability --- go.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'go.html.markdown') diff --git a/go.html.markdown b/go.html.markdown index 34b855e3..61ba9c42 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -10,6 +10,7 @@ contributors: - ["Quint Guvernator", "https://github.com/qguv"] - ["Jose Donizetti", "https://github.com/josedonizetti"] - ["Alexej Friesen", "https://github.com/heyalexej"] + - ["Clayton Walker", "https://github.com/cwalk"] --- Go was created out of the need to get work done. It's not the latest trend @@ -115,7 +116,7 @@ can include line breaks.` // Same string type. fmt.Println(s) // Updated slice is now [1 2 3 4 5 6] // To append another slice, instead of list of atomic elements we can // pass a reference to a slice or a slice literal like this, with a - // trailing elipsis, meaning take a slice and unpack its elements, + // trailing ellipsis, meaning take a slice and unpack its elements, // appending them to slice s. s = append(s, []int{7, 8, 9}...) // Second argument is a slice literal. fmt.Println(s) // Updated slice is now [1 2 3 4 5 6 7 8 9] @@ -129,7 +130,7 @@ can include line breaks.` // Same string type. m["one"] = 1 // Unused variables are an error in Go. - // The underbar lets you "use" a variable but discard its value. + // The underscore lets you "use" a variable but discard its value. _, _, _, _, _, _, _, _, _, _ = str, s2, g, f, u, pi, n, a3, s4, bs // Output of course counts as using a variable. fmt.Println(s, c, a4, s3, d2, m) @@ -164,7 +165,7 @@ func expensiveComputation() float64 { } func learnFlowControl() { - // If statements require brace brackets, and do not require parens. + // If statements require brace brackets, and do not require parentheses. if true { fmt.Println("told ya") } -- cgit v1.2.3 From 56ec0882a4e9c1e13dfbff20b4d15470c130c1f3 Mon Sep 17 00:00:00 2001 From: Clayton Walker Date: Wed, 7 Oct 2015 00:07:00 -0400 Subject: Added more to further reading --- go.html.markdown | 2 ++ 1 file changed, 2 insertions(+) (limited to 'go.html.markdown') diff --git a/go.html.markdown b/go.html.markdown index 61ba9c42..f9821a0c 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -408,6 +408,8 @@ func requestServer() { The root of all things Go is the [official Go web site](http://golang.org/). There you can follow the tutorial, play interactively, and read lots. +Aside from a tour, [the docs](https://golang.org/doc/) contain how to write +clean and effective Go code, package and command docs, and release history. The language definition itself is highly recommended. It's easy to read and amazingly short (as language definitions go these days.) -- cgit v1.2.3 From bc065831ce25467ba06d3cf6e6ad159eed16a525 Mon Sep 17 00:00:00 2001 From: Clayton Walker Date: Thu, 8 Oct 2015 23:24:25 -0400 Subject: Added suggested changes --- go.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'go.html.markdown') diff --git a/go.html.markdown b/go.html.markdown index f9821a0c..646a5650 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -408,8 +408,8 @@ func requestServer() { The root of all things Go is the [official Go web site](http://golang.org/). There you can follow the tutorial, play interactively, and read lots. -Aside from a tour, [the docs](https://golang.org/doc/) contain how to write -clean and effective Go code, package and command docs, and release history. +Aside from a tour, [the docs](https://golang.org/doc/) contain information on +how to write clean and effective Go code, package and command docs, and release history. The language definition itself is highly recommended. It's easy to read and amazingly short (as language definitions go these days.) -- cgit v1.2.3 From da556f64f982e11a299db6aa38ab4c260eab11d9 Mon Sep 17 00:00:00 2001 From: Romin Irani Date: Wed, 14 Oct 2015 12:03:05 +0530 Subject: Added Go Mobile information --- go.html.markdown | 2 ++ 1 file changed, 2 insertions(+) (limited to 'go.html.markdown') diff --git a/go.html.markdown b/go.html.markdown index 646a5650..a857a76c 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -423,3 +423,5 @@ idioms. Or you can click on a function name in [the documentation](http://golang.org/pkg/) and the source code comes up! Another great resource to learn Go is [Go by example](https://gobyexample.com/). + +Go Mobile adds support for mobile platforms (Android and iOS). You can write all-Go native mobile apps or write a library that contains bindings from a Go package, which can be invoked via Java (Android) and Objective-C (iOS). Check out the [Go Mobile page](https://github.com/golang/go/wiki/Mobile) for more information. -- cgit v1.2.3 From 4de15e41b3d47963ab7749a6a4dc6704d5d3af27 Mon Sep 17 00:00:00 2001 From: Robert Brown Date: Sat, 24 Oct 2015 13:56:20 +0100 Subject: correct minor grammar and formatting to improve readability --- 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 a857a76c..dc684227 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -108,12 +108,13 @@ can include line breaks.` // Same string type. bs := []byte("a slice") // Type conversion syntax. // Because they are dynamic, slices can be appended to on-demand. - // To append elements to a slice, built-in append() function is used. + // To append elements to a slice, the built-in append() function is used. // First argument is a slice to which we are appending. Commonly, // the array variable is updated in place, as in example below. s := []int{1, 2, 3} // Result is a slice of length 3. s = append(s, 4, 5, 6) // Added 3 elements. Slice now has length of 6. fmt.Println(s) // Updated slice is now [1 2 3 4 5 6] + // To append another slice, instead of list of atomic elements we can // pass a reference to a slice or a slice literal like this, with a // trailing ellipsis, meaning take a slice and unpack its elements, -- cgit v1.2.3 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 From cf976f3fa7d3863a3cc9cd8726379ec20319002d Mon Sep 17 00:00:00 2001 From: Alexsander Akers Date: Fri, 4 Aug 2017 15:40:52 +0200 Subject: Fix typo in Go comment --- 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 50692f9c..e5263cf6 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -99,7 +99,7 @@ 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. - a5 := [...]int{3, 1, 5, 10, 100} // An array initialized with a fixed size of fize + a5 := [...]int{3, 1, 5, 10, 100} // An array initialized with a fixed size of five // elements, with values 3, 1, 5, 10, and 100. // Slices have dynamic size. Arrays and slices each have advantages -- cgit v1.2.3