[go/en] update for modern go (#4678)

This commit is contained in:
Sean Liao 2024-05-15 03:52:23 +01:00 committed by GitHub
parent 69f7f5d856
commit 250a508cbf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -32,11 +32,11 @@ Go comes with a good standard library and a sizeable community.
/* Multi- /* Multi-
line comment */ line comment */
/* A build tag is a line comment starting with // +build /* A build tag is a line comment starting with //go:build
and can be executed by go build -tags="foo bar" command. and can be executed by go build -tags="foo bar" command.
Build tags are placed before the package clause near or at the top of the file Build tags are placed before the package clause near or at the top of the file
followed by a blank line or other line comments. */ followed by a blank line or other line comments. */
// +build prod, dev, test //go:build prod || dev || test
// A package clause starts every source file. // A package clause starts every source file.
// main is a special name declaring an executable rather than a library. // main is a special name declaring an executable rather than a library.
@ -45,9 +45,10 @@ package main
// Import declaration declares library packages referenced in this file. // Import declaration declares library packages referenced in this file.
import ( import (
"fmt" // A package in the Go standard library. "fmt" // A package in the Go standard library.
"io/ioutil" // Implements some I/O utility functions. "io" // 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!
_ "net/http/pprof" // Profiling library imported only for side effects
"os" // OS functions like working with the file system "os" // OS functions like working with the file system
"strconv" // String conversions. "strconv" // String conversions.
) )
@ -355,7 +356,7 @@ func learnInterfaces() {
} }
// Functions can have variadic parameters. // Functions can have variadic parameters.
func learnVariadicParams(myStrings ...interface{}) { func learnVariadicParams(myStrings ...any) { // any is an alias for interface{}
// Iterate each value of the variadic. // Iterate each value of the variadic.
// The underscore here is ignoring the index argument of the array. // The underscore here is ignoring the index argument of the array.
for _, param := range myStrings { for _, param := range myStrings {
@ -428,7 +429,6 @@ func learnConcurrency() {
// A single function from package http starts a web server. // A single function from package http starts a web server.
func learnWebProgramming() { func learnWebProgramming() {
// First parameter of ListenAndServe is TCP address to listen to. // First parameter of ListenAndServe is TCP address to listen to.
// Second parameter is an interface, specifically http.Handler. // Second parameter is an interface, specifically http.Handler.
go func() { go func() {
@ -449,7 +449,7 @@ func requestServer() {
resp, err := http.Get("http://localhost:8080") resp, err := http.Get("http://localhost:8080")
fmt.Println(err) fmt.Println(err)
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
fmt.Printf("\nWebserver said: `%s`", string(body)) fmt.Printf("\nWebserver said: `%s`", string(body))
} }
``` ```
@ -457,20 +457,23 @@ func requestServer() {
## Further Reading ## Further Reading
The root of all things Go is the [official Go web site](https://go.dev/). The root of all things Go is the [official Go web site](https://go.dev/).
There you can follow the tutorial, play interactively, and read lots. There you can follow the [tutorial](https://go.dev/tour/), play interactively, and read lots.
Aside from a tour, [the docs](https://go.dev/doc/) contain information on Aside from a tour, [the docs](https://go.dev/doc/) contain information on
how to write clean and effective Go code, package and command docs, and release history. how to write clean and effective Go code, package and command docs, and release history.
The [Go language specification](https://go.dev/ref/spec) itself is highly recommended. It's easy to read The [Go language specification](https://go.dev/ref/spec) itself is highly recommended. It's easy to read
and amazingly short (as language definitions go these days.) and amazingly short (as language definitions go these days.)
You can play around with the code on [Go playground](https://go.dev/play/p/tnWMjr16Mm). Try to change it and run it from your browser! Note that you can use [https://go.dev/play/](https://go.dev/play/) as a [REPL](https://en.wikipedia.org/wiki/Read-eval-print_loop) to test things and code in your browser, without even installing Go. You can play around with the code on [Go playground](https://go.dev/play/p/Y96bRpJWzjr).
Try to change it and run it from your browser!
Note that you can use [https://go.dev/play/](https://go.dev/play/)
as a [REPL](https://en.wikipedia.org/wiki/Read-eval-print_loop) to test things and code in your browser, without even installing Go.
On the reading list for students of Go is the [source code to the standard On the reading list for students of Go is the [source code to the standard
library](https://go.dev/src/). Comprehensively documented, it library](https://go.dev/src/). Comprehensively documented, it
demonstrates the best of readable and understandable Go, Go style, and Go demonstrates the best of readable and understandable Go, Go style, and Go
idioms. Or you can click on a function name in [the idioms. Or you can click on a function name in [the
documentation](https://go.dev/pkg/) and the source code comes up! documentation](https://pkg.go.dev/std) and the source code comes up!
Another great resource to learn Go is [Go by example](https://gobyexample.com/). Another great resource to learn Go is [Go by example](https://gobyexample.com/).
@ -480,4 +483,6 @@ There are many excellent conference talks and video tutorials on Go available on
- [Golang University 201](https://www.youtube.com/playlist?list=PLEcwzBXTPUE_5m_JaMXmGEFgduH8EsuTs) steps it up a notch, explaining important techniques like testing, web services, and APIs - [Golang University 201](https://www.youtube.com/playlist?list=PLEcwzBXTPUE_5m_JaMXmGEFgduH8EsuTs) steps it up a notch, explaining important techniques like testing, web services, and APIs
- [Golang University 301](https://www.youtube.com/playlist?list=PLEcwzBXTPUE8KvXRFmmfPEUmKoy9LfmAf) dives into more advanced topics like the Go scheduler, implementation of maps and channels, and optimisation techniques - [Golang University 301](https://www.youtube.com/playlist?list=PLEcwzBXTPUE8KvXRFmmfPEUmKoy9LfmAf) dives into more advanced topics like the Go scheduler, implementation of maps and channels, and optimisation techniques
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. 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.