Fundamentals 9 min read

Quick Introduction to Go Language: Installation, Data Types, Control Structures, Functions, and Concurrency

This article provides a concise Go language tutorial covering installation, environment configuration, basic data types, control flow statements, function definitions with multiple returns and variadic parameters, and a simple goroutine concurrency example, enabling beginners to quickly grasp Go's core concepts.

TAL Education Technology
TAL Education Technology
TAL Education Technology
Quick Introduction to Go Language: Installation, Data Types, Control Structures, Functions, and Concurrency

Go is a statically typed, compiled, concurrent programming language developed by Google, offering garbage collection and a simple syntax with only 25 keywords.

Environment Setup

Download the Go binary from https://golang.google.cn/dl/ , extract it (e.g., go1.18.darwin-amd64.tar.gz) to $HOME/Documents/go1.18, and configure the environment variables:

export GOROOT=$HOME/Documents/go1.18
export PATH=$PATH:$GOROOT/bin
export GOPATH=$HOME/Documents/gopath

Verify the installation with go version. An IDE such as GoLand ( https://www.jetbrains.com/go/ ) is recommended.

Hello World

package main
import "fmt"
func main() {
    fmt.Println("hello world")
}

The file must declare its package (e.g., package main) and the main function serves as the entry point.

Data Types

package main

import "fmt"

func main() {
    // variable declaration
    var a int = 1 // type can be omitted
    b := 1          // short declaration
    b = 3           // re‑assign
    fmt.Println(a, b)

    // strings
    str1 := "hello "
    str2 := "world"
    fmt.Println(len(str1), str1+str2)

    // array (fixed size)
    arr := [5]int{1,2,3,4,5}
    arr[1] = 100
    fmt.Println(len(arr), arr)

    // slice (dynamic array)
    slice := []int{1,2,3}
    slice[1] = 100
    slice = append(slice, 4,5,6)
    fmt.Println(len(slice), cap(slice), slice)

    // map (key‑value)
    score := map[string]int{"zhangsan":100, "lisi":99, "wangwu":98}
    score["who"] = 90
    s, ok := score["who"]
    delete(score, "lisi")
    fmt.Println(s, ok, score)
}

Go also supports custom types such as structs and interfaces, which are introduced later.

Branch Structures

package main

import "fmt"

func main() {
    // if‑else
    condition := true
    if condition {
        fmt.Println("true")
    } else {
        fmt.Println("false")
    }

    // switch
    expr := "zhangsan"
    switch expr {
    case "zhangsan":
        fmt.Println("zhangsan")
    case "lisi":
        fmt.Println("lisi")
    default:
        fmt.Println("who")
    }

    // for loop
    for i := 0; i < 100; i++ {
        if i%2 == 0 {
            fmt.Println("偶数")
        }
    }

    // infinite loop with break
    i := 0
    for {
        i++
        fmt.Println("loop")
        if i > 100 {
            break
        }
    }
}

Functions

package main

import "fmt"

func main() {
    add, sub := addSub(4, 3)
    fmt.Println(add, sub)
    sum(1, 2, 3)
    nums := []int{1,2,3,4}
    sum(nums...)

    // function variable
    f := func(in string) {
        fmt.Println(in)
    }
    f("hello world")

    // immediate anonymous function execution
    func(in string) {
        fmt.Println(in)
    }("hello world")
}

// returns two ints
func addSub(a, b int) (int, int) {
    return a + b, a - b
}

// variadic parameters
func sum(nums ...int) {
    total := 0
    for _, num := range nums {
        total += num
    }
    fmt.Println(total)
}

Goroutine Concurrency

package main

import (
    "fmt"
    "sync"
)

func main() {
    var wg sync.WaitGroup
    for i := 0; i < 10; i++ {
        wg.Add(1)
        go func(a int) {
            fmt.Println(fmt.Sprintf("work %d exec", a))
            wg.Done()
        }(i)
    }
    wg.Wait()
    fmt.Println("main end")
}

This program launches ten goroutines that print a message, uses a sync.WaitGroup to wait for all goroutines to finish, and then exits.

Conclusion

The article briefly introduced Go's basic syntax, data types, control structures, functions, and concurrency model, providing a foundation for further exploration of the language.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

data typesinstallationFundamentals
TAL Education Technology
Written by

TAL Education Technology

TAL Education is a technology-driven education company committed to the mission of 'making education better through love and technology'. The TAL technology team has always been dedicated to educational technology research and innovation. This is the external platform of the TAL technology team, sharing weekly curated technical articles and recruitment information.

0 followers
Reader feedback

How this landed with the community

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.