Go Language Beginner's Guide: Installation, Syntax, and Core Concepts
This three‑day learning note walks readers through installing Go, setting up the environment, writing a hello‑world program, using modules, core syntax such as variables, constants, slices and structs, defining functions and methods, employing interfaces and embedding for OOP, and demonstrating goroutine‑based concurrency with channels.
This article is a personal learning note that records the author’s three‑day entry into Go language development, comparing it with Java and JavaScript and focusing on the language’s design ideas and practical usage.
Environment Setup
Download the Go installer for macOS, install it, and verify the version with go version. Configure the environment variables in .bash_profile:
export GOROOT=/usr/local/go
export GOPATH=$HOME/goGOROOT points to the Go toolchain, similar to JAVA_HOME, while GOPATH is the workspace for Go projects.
First Program
Create a directory helloworld and a file hello.go:
package main
import "fmt"
func main() {
fmt.Println("hello, world")
}Compile with go build hello.go or run directly with go run hello.go. The entry package must be main, otherwise the binary cannot be executed.
Go Modules
Since Go 1.11 (default from 1.13) modules replace GOPATH for dependency management. Initialise a module with:
go mod init calc-mod
# or go mod init github.com/username/projectDependencies are fetched into $GOPATH/pkg/mod automatically.
Language Syntax
Packages and Imports : Go uses the folder name as the package name; multiple packages can share the same name and are imported without a class‑level qualifier.
Variables : Declared with var or the short form :=. Types appear after the variable name, and the semicolon is optional.
var v1 int = 10 // explicit type
var v2 = 10 // type inferred
v3 := 10 // short declarationConstants and iota provide enumerations:
const (
Sunday = iota
Monday
Tuesday
// ...
)Composite Types include pointers, arrays, slices, maps, structs, and interfaces. Example of a slice:
var f = make([]string, 3)
f = append(f, "four")Nil is a typed zero value used for pointers, slices, maps, etc., and its comparison follows Go’s rules.
Functions
Functions are declared with the func keyword. They can return multiple values, and named return values allow an implicit return statement.
func GetEventHandleMsg(code int) (string, error) {
// ...
return msg, nil
}Anonymous functions and closures are first‑class citizens and can be passed as arguments:
add := func(a, b int) {
fmt.Println(a + b)
}
callback(1, add)Variadic functions use ... both in definition and call.
func SkipHandler(c *gin.Context, skippers ...SkipperFunc) bool {
for _, skipper := range skippers {
if skipper(c) { return true }
}
return false
}Object‑Oriented Features
Go does not have a class keyword; structs serve as data containers. Methods are defined by attaching a receiver to a function:
type Student struct { id uint; name string }
func (s Student) GetName() string { return s.name }
func (s *Student) SetName(name string) { s.name = name }Inheritance is achieved through embedding (composition). An embedded struct’s fields and methods are promoted to the outer struct, allowing method overriding.
type Animal struct { name string }
func (a Animal) Call() string { return "Voice..." }
type Dog struct { Animal }
func (d Dog) Call() string { return "Woof!" }Interfaces are satisfied implicitly; a type implements an interface by providing all its methods.
type Phone interface { Call() }
type IPhone struct { name string }
func (p IPhone) Call() { fmt.Println("Iphone calling.") }Concurrency
Go’s concurrency model is based on goroutines and channels. A classic producer‑consumer example demonstrates a string channel, multiple producers, and a single consumer:
func producer(header string, ch chan<- string) {
for {
ch <- fmt.Sprintf("%s: %v", header, rand.Int31())
time.Sleep(time.Second)
}
}
func consumer(ch <-chan string) {
for {
msg := <-ch
fmt.Println(msg)
}
}
func main() {
ch := make(chan string)
go producer("cat", ch)
go producer("dog", ch)
consumer(ch)
}This prints interleaved messages from the two producers.
Conclusion
The note covers Go installation, basic syntax, data structures, functions, OOP concepts, and a simple concurrency pattern, providing a solid foundation for further exploration of topics such as context handling, error handling, web development, and database access.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Amap Tech
Official Amap technology account showcasing all of Amap's technical innovations.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
