Go Tutorial
Get productive in Go — packages, structs, interfaces, goroutines, and a tiny HTTP server.
Go (Golang) is built for simplicity and concurrency. It compiles to a single binary, which makes deployment trivial.
Hello, Go
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
Run with go run main.go. Build a binary with go build.
Types and structs
type User struct {
ID int
Name string
}
func (u User) Greet() string {
return "Hi, " + u.Name
}
Interfaces
type Speaker interface {
Speak() string
}
func Announce(s Speaker) {
fmt.Println(s.Speak())
}
Interfaces are satisfied implicitly — no
implementskeyword. This keeps dependencies loose.
Concurrency with goroutines
func fetch(url string, ch chan<- string) {
ch <- "done: " + url
}
func main() {
ch := make(chan string, 3)
for _, u := range []string{"a", "b", "c"} {
go fetch(u, ch)
}
for i := 0; i < 3; i++ {
fmt.Println(<-ch)
}
}
A minimal HTTP server
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello from Go!")
})
http.ListenAndServe(":8080", nil)
}
Visit http://localhost:8080 after running it.