Fundamentals 24 min read

Go Language Beginner's Guide: Installation, Environment, Syntax, and Concurrency

This article provides a comprehensive beginner‑friendly walkthrough of Go, covering its origins, installation, environment variables, IDE setup, project structure, core syntax, data types, functions, object‑oriented concepts, and a practical concurrency example using goroutines and channels.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Go Language Beginner's Guide: Installation, Environment, Syntax, and Concurrency

In the preface the author explains the motivation for learning Go after years of Java and JavaScript development, emphasizing that a language is just a tool and that the focus should be on its design philosophy.

1. Introduction – Go was created by Ken Thompson, Rob Pike, and Robert Griesemer at Google in 2007 and released in 2009. It aims to combine the rapid development speed of dynamic languages with the performance and safety of compiled languages, offering simple deployment, strong concurrency via goroutines, and good execution performance.

2. Environment – Install the Go SDK from the official website (e.g., the macOS .pkg file) and verify the installation with go version . Set the required environment variables in .bash_profile :

export GOROOT=/usr/local/go
export GOPATH=$HOME/go

GOROOT points to the Go installation (similar to JAVA_HOME ), while GOPATH is the workspace for Go projects and downloaded dependencies.

3. IDEs

GoLand – provides automatic imports, build configurations, and Go module support.

VS Code – install the official Go extension to get language features.

4. Project Structure – Under GOPATH the workspace contains three directories: src (source code), bin (executables), and pkg (compiled packages). A typical layout looks like:

├── bin
│   ├── air
│   ├── govendor
│   ├── swag
│   └── wire
├── pkg
│   ├── darwin_amd64
│   ├── mod
│   └── sumdb
└── src
    ├── calc
    ├── gin-blog
    ├── github.com
    ├── golang.org
    ├── google.golang.org
    ├── gopkg.in
    └── simplemath

Using Go modules (available since Go 1.11 and enabled by default in 1.13) eliminates the need for GOPATH‑based workspace; a module is created with go mod init <module‑name> and dependencies are fetched automatically.

5. Core Syntax

Packages and imports – The package name matches the folder name; import statements can list multiple packages inside parentheses.

Variables – Declared with var or using type inference with := . Types appear after the variable name.

Constants and iota – Define enumerations with const ( … ) and the auto‑incrementing iota .

Basic types – bool, int, uint, float, complex, string, rune, error.

Composite types – pointer, array, slice, map, chan, struct, interface.

Type conversion – Use the target type as a function, e.g., int(v) or string([]byte{'h','i'}) .

6. Data Structures

Arrays – fixed length, e.g., var a [8]byte .

Slices – dynamic view of an array, created with make([]int, 5) or literals.

Maps – key/value collections, declared with var m map[string]int or make(map[string]int) .

make vs new – make initializes slices, maps, and channels; new allocates zeroed memory and returns a pointer.

7. Nil and Zero Values – nil is a typed zero value for pointers, slices, maps, channels, interfaces, and function types. Unlike Java’s null , a Go string cannot be nil ; it is empty ( "" ).

8. Control Flow

If statements omit parentheses; optional init statement can appear before the condition.

Switch can be expression‑less, support multiple case values, fallthrough, and function calls.

Only for exists for loops (including while‑style and infinite loops).

Goto is still available, though rarely used.

Defer schedules a function call to run after the surrounding function returns, similar to a finally block.

9. Functions

Declared with func . Functions can return multiple values.

Variadic parameters use ... (e.g., func f(a ...int) ).

Anonymous functions and closures are first‑class citizens.

10. Object‑Oriented Features

Structs replace classes; methods are defined with a receiver before the method name.

Embedding (composition) provides a form of inheritance; methods of the embedded type are promoted.

Interfaces are implicit – a type implements an interface by providing the required methods.

11. Concurrency

A simple producer‑consumer example demonstrates goroutine creation and channel communication:

// producer
func producer(header string, ch chan<- string) {
    for {
        ch <- fmt.Sprintf("%s: %v", header, rand.Int31())
        time.Sleep(time.Second)
    }
}

// consumer
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 program launches two producers that send timestamped strings to a shared channel, while the consumer continuously reads and prints them, illustrating Go’s lightweight concurrency model.

12. Summary – The article serves as a concise entry point to Go, covering installation, workspace setup, core language constructs, object‑oriented patterns, and basic concurrency, with pointers to further topics such as context, error handling, web frameworks, and database access.

concurrencyprogrammingGoTutorialSyntax
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.