Fundamentals 27 min read

How I Learned Go in 3 Days: A Practical Jump from Java to Go

This article walks through a rapid three‑day Go onboarding, covering installation, environment setup, core syntax, package management with Go modules, common development tools, and a simple producer‑consumer concurrency example, while comparing Go concepts to Java and JavaScript.

Alibaba Cloud Developer
Alibaba Cloud Developer
Alibaba Cloud Developer
How I Learned Go in 3 Days: A Practical Jump from Java to Go

Preface

I was originally a Java developer working on JavaWeb, later moved to Android (still Java), and eventually to front‑end development using JavaScript. When I joined Alibaba Cloud, project needs forced me to pick up Go. I believe a language is just a tool; the important part is its thinking and logic, so I spent three days learning Go by comparing its syntax to Java and JavaScript.

Introduction

Go was created by Ken Thompson, Rob Pike, and Robert Griesemer at Google in 2007 and released publicly in 2009. Its main goal is to combine the development speed of dynamic languages like Python with the performance and safety of compiled languages such as C/C++. Go emphasizes simplicity, easy deployment, good concurrency (goroutine‑based), solid language design, and strong execution performance.

Environment

Download the macOS pkg from the official site, install it, and verify the installation with go version.

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

GOROOT points to the Go installation directory (similar to JAVA_HOME).

GOPATH is the workspace for Go projects; it holds source, binaries, and compiled packages.

1 Plain Text Development

Create a directory, add 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 resulting binary can be executed without a virtual machine.

2 GoLand

GoLand provides auto‑import, build configurations similar to Android/Java, and easy module synchronization.

3 VSCode

Install the Go extension from the marketplace; it offers code completion, debugging, and other language features.

Project Structure

Under GOPATH there are three sub‑directories: bin (executables), pkg (compiled packages), and src (source files). With Go modules the workspace is no longer tied to GOPATH.

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

In practice, GOPATH should hold shared libraries and dependencies, while each project can have its own isolated workspace via Go modules.

Go Modules

Since Go 1.11, modules are supported and enabled by default in 1.13. Create a module with go mod init; dependencies are stored in GOPATH/pkg/mod. Modules allow you to create a project anywhere without relying on GOPATH.

// create a module
go mod init calc-mod
// or use a full repository path
go mod init github.com/fuxing-repo/fuxing-module-name

To download dependencies, run go get -u github.com/fuxing-repo/fuxing-module-name.

Syntax

Package and Import

Package name is the folder name; you can import a package using its full path or an alias. Unlike Java, you import the whole package, not individual classes.

Variables

Declare variables with var or use the short form :=. Types appear after the variable name.

var v1 int = 10   // explicit type
var v2 = 10       // type inferred
v3 := 10          // short declaration, type inferred

Multiple Assignment

i, j = j, i

This enables swapping values and returning multiple results from functions.

Anonymous Variables

Use _ to discard values you do not need.

Pointers

Go’s pointer semantics are similar to C, providing direct memory access for performance and enabling efficient data sharing.

Types

Basic types: bool, integer families ( int8, uint, etc.), floating‑point ( float32, float64), complex ( complex64, complex128), string, rune (alias for uint32), and error. Composite types: pointer, array, slice, map, channel, struct, interface.

Constants and iota

const (
    Sunday = iota
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    numberOfDays
)

Type Conversion

v1 := 99.99
v2 := int(v1)          // 99
v1 := []byte{'h','e','l','l','o'}
v2 := string(v1)      // "hello"
// string to int
v1 := "100"
v2, err := strconv.Atoi(v1) // 100

Arrays and Slices

// array
var a [8]byte
// slice
b := []int{}
b = append(b, 4)

Maps

var testMap map[string]int
testMap = map[string]int{"one":1,"two":2,"three":3}
// or using make
testMap = make(map[string]int)
testMap["one"] = 1

Make and New

// make creates slices, maps, or channels
s := make([]int, 5)
m := make(map[string]int)
c := make(chan int, 10)
// new returns a pointer to a zero value
p := new(int)

Control Flow

If statements omit parentheses; switch can be expression‑less and supports fallthrough.

score := 100
switch score {
case 90, 100:
    fmt.Println("Grade: A")
case 80:
    fmt.Println("Grade: B")
default:
    fmt.Println("Grade: F")
}

Loops

Only for exists; it can act as a traditional C‑style loop, a while‑style loop, or an infinite loop.

for i := 1; i <= 5; i++ { fmt.Println(i) }
for a <= 5 { fmt.Println(a); a++ }
for { /* infinite */ }
for idx, item := range listArray { fmt.Printf("hello, %d, %s
", idx, item) }

Goto and Defer

Go still supports goto. defer schedules a function call to run after the surrounding function returns, similar to Java’s finally.

defer fmt.Println("end")

Concurrency

A simple producer‑consumer example demonstrates goroutine scheduling 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)
}

Conclusion

This guide provides a quick start to Go, covering installation, environment variables, basic syntax, module management, common development tools, and a simple concurrency pattern. Future posts will explore context handling, error management, web frameworks, database integration, and more advanced Go features.

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.

BackendconcurrencyprogrammingModules
Alibaba Cloud Developer
Written by

Alibaba Cloud Developer

Alibaba's official tech channel, featuring all of its technology innovations.

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.