1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-08 07:46:46 +02:00

[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
This commit is contained in:
Leonid Shevtsov
2016-10-31 19:08:32 +02:00
committed by ven
parent 092b7a69b1
commit 5f9d8fe4e0

View File

@@ -11,6 +11,7 @@ contributors:
- ["Jose Donizetti", "https://github.com/josedonizetti"] - ["Jose Donizetti", "https://github.com/josedonizetti"]
- ["Alexej Friesen", "https://github.com/heyalexej"] - ["Alexej Friesen", "https://github.com/heyalexej"]
- ["Clayton Walker", "https://github.com/cwalk"] - ["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 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. "io/ioutil" // Implements some I/O utility functions.
m "math" // Math library with local alias m. m "math" // Math library with local alias m.
"net/http" // Yes, a web server! "net/http" // Yes, a web server!
"os" // OS functions like working with the file system
"strconv" // String conversions. "strconv" // String conversions.
) )
@@ -133,6 +135,14 @@ can include line breaks.` // Same string type.
// Unused variables are an error in Go. // Unused variables are an error in Go.
// The underscore 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 _, _, _, _, _, _, _, _, _, _ = 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. // Output of course counts as using a variable.
fmt.Println(s, c, a4, s3, d2, m) fmt.Println(s, c, a4, s3, d2, m)
@@ -211,6 +221,10 @@ func learnFlowControl() {
// for each pair in the map, print key and value // for each pair in the map, print key and value
fmt.Printf("key=%s, value=%d\n", key, 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 // As with for, := in an if statement means to declare and assign
// y first, then test y > x. // y first, then test y > x.