|
- Go by Example
Go by Example is a hands-on introduction to Go using annotated example programs Check out the first example or browse the full list below Unless stated otherwise, examples here assume the latest major release Go and may use new language features
- Hello World - Go by Example
To run the program, put the code in hello-world go and use go run $ go run hello-world go hello world Sometimes we’ll want to build our programs into binaries We can do this using go build $ go build hello-world go $ ls hello-world hello-world go We can then execute the built binary directly
- Go by Example: Slices
Go by Example: Slices Slices are an important data type in Go, giving a more powerful interface to sequences than arrays package main: import ("fmt" "slices") func main {Unlike arrays, slices are typed only by the elements they contain (not the number of elements) An uninitialized slice equals to nil and has length 0
- : Maps - Go by Example
Go by Example: Maps Maps are Go’s built-in associative data type (sometimes called hashes or dicts in other languages) package main: import ("fmt" "maps") func main {To create an empty map, use the builtin make: make(map[key-type]val-type) m:= make (map [string] int)
- Go by Example: Arrays
Go by Example: Arrays In Go, an array is a numbered sequence of elements of a specific length In typical Go code, slices are much more common; arrays are useful in some special scenarios
- Go by Example: Channels
Go by Example: Channels Channels are the pipes that connect concurrent goroutines You can send values into channels from one goroutine and receive those values into another goroutine
- Go by Example: For
for is Go’s only looping construct Here are some basic types of for loops package main: import "fmt": func main {: The most basic type, with a single condition i:= 1 for i <= 3 {fmt Println (i) i = i + 1}: A classic initial condition after for loop for j:= 0; j < 3; j ++ {fmt Println (j)}: Another way of accomplishing the basic “do this N times” iteration is range over an integer
|
|
|