- Getting Started
- Download and install
- Tutorial: Get started with Go
- Tutorial: Create a Go Module
- Tutorial: Getting started with multi-module workspaces
- Tutorial: Developing a RESTful API with Go and Gin
- Tutorial: Getting started with generics
- Tutorial: Getting started with fuzzing
- Writing Web Applications
- How to Write Go Code
- the hello project
go.mod: manage dependenciesgo mod init: initialize a go projectgo mod tidy: automatically manage modules
In a module, one or more related packages are included for a discrete and
useful set of functions.
package main: In Go, code executed as an application must be in amainpackage.
- Go code is grouped into packages, and packages are grouped into modules.
That means
module>package. - Dependencies including Go version are specified at the module level.
go get .
go mod downloadhttps://stackoverflow.com/a/66584699/7267801
Your module's
go.modfile records which versions of dependencies it requires. The source code for those dependencies is stored in a local cache.
go getupdates the requirements listed in yourgo.modfile. It also ensures that those requirements are self-consistent, and adds new requirements as needed so that every package imported by the packages you named on the command line is provided by some module in your requirements.As a side-effect of updating and adding requirements, go get also downloads the modules containing the named packages (and their dependencies) to the local module cache.
In contrast,
go mod downloaddoes not add new requirements or update existing requirements. (At most, it will ensure that the existing requirements are self-consistent, which can occur if you have hand-edited thego.modfile.) It only downloads either the specific module versions you've requested (if you requested specific versions), or the versions of modules that appear in your requirements.
func Hello(name string) string {}
----- ------ ------
// Function Parameter Return
// name type type-
A function whose name starts with a capital letter can be called by a function not in the same package. This is known in Go as an exported name.
-
The
:=operator is a shortcut for declaring and initializing a variable in one line. Go uses the value on the right to determine the variable's type.It's equivalent to:
var message string message = fmt.Sprintf("Hi, %v. Welcome!", name)
For module that has not been published yet, we could use it in local by replacing module path.
go mod edit -replace example.com/greetings=../greetingsThen use
go mod tidyto install modules.
replace example.com/greetings => ../greetings
require example.com/greetings v0.0.0-00010101000000-000000000000- the
replacedirective - the
requiredirective
The number following the module path is a pseudo-version number -- a generated number used in place of a semantic version number (which the module doesn't have yet).
func Hello(name string) (string, error) {
// If no name was given, return an error with a message.
if name == "" {
return "", errors.New("empty name")
}
}- Any Go function can return multiple values.
log.Fatal(): print the error and stop the program.
- A slice is like an array, except that its size changes dynamically as you add and remove items.
[]: slice, omiting its size in the brackets tells Go that the size of the array underlying the slice can be dynamically changed
[]string{
"a",
"b"
}- Go executes init functions automatically at program startup, after global
- variables have been initialized.
- In Go, you initialize a
mapwith the following syntax:make(map[key-type]value-type) range iterablesreturns two values:- the index of the current item
- a copy of the item's value
_: the Go blank identifier
- file naming convention:
*_test.go - case naming convention:
func TestName(t *testing.T)- the parameter is a
pointerto thetesting.Ttype, we use its methods for reporting and logging.
- the parameter is a
go test: to execute the tests
go build: compiles the packages, along with their dependencies, generates an executablego install: compiles and installs the packagesgo list -f '{{.Target}}': find out where the go command will install the current packagego env -w GOBIN=/path/to/your/bin: customize go installation target directory
go work init PATHThe go.work file has similar syntax to go.mod.
go 1.18
use ./hello
- The
godirective tells Go the version. - The
usedirective tells Go that the module in./helloshould be used as main modules.
/albumsGET- Get a list of all albums, returned asJSON.POST- Add a new album from request data sent asJSON.
/albums/:idGET- Get an album by its ID, returning the album data asJSON.
// album represents data about a record album.
type album struct {
ID string `json:"id"`
Title string `json:"title"`
Artist string `json:"artist"`
Price float64 `json:"price"`
}Struct tags such as json:"artist" specify what a field’s name should be when
the struct’s contents are serialized into JSON.
Without them, the JSON would use the struct’s capitalized field names – a style not as common in JSON.
https://go.dev/doc/tutorial/generics
progress
https://go.dev/doc/tutorial/fuzz
progress