Advantage of Golang

  • Effortless Deployment and Execution
    • Direct compilation to machine code for streamlined execution.
    • Independence from external dependencies ensures a lightweight footprint.
    • Straightforward run and deployment process, unlike Java which requires JDK.
  • Static Typing
    • Compile-time error detection enhances code reliability.
  • Robust Concurrency Support
    • Intrinsic support for concurrent programming.
    • Efficient utilization of multi-core processors.
  • Extensive Library Support
    • Advanced runtime system scheduling for optimal performance.
    • State-of-the-art garbage collection mechanisms.
    • Comprehensive suite of standard libraries.
  • User-Friendly Learning Curve
    • Minimalistic design with only 25 keywords.
    • Familiar C-like syntax for easy adaptation.
    • Incorporates object-oriented programming principles.
    • Cross-platform compatibility broadens usability.
  • Widespread Industry Adoption
    • Utilized by tech giants like Google, Meta, Tencent, TikTok/Bytedance, and Baidu.
    • Popular products like docker, kubernetes

Hello World Demo

Create a file called helloworld.go

1
2
3
4
5
6
7
8
package main // package declaration

import "fmt" // import statement

// main function
func main() {
fmt.Println("Hello World!")
}

Multiple Import

Multiple libraries can be imported within one single import

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package main // package declaration

/*
import "fmt" // import statement
import "time"
*/

import (
"fmt"
"time"
)

// main function
func main() {
fmt.Println("Hello World!")
time.Sleep(1 * time.Second)
}

Curly Braces Syntax

Note that in Go, the curly braces must be on the same line as the function declaration. The following code example won’t work

1
2
3
4
5
6
// Curly Braces Syntax Error example
func main()
{
fmt.Println("Hello World!")
time.Sleep(1 * time.Second)
}

The program will complain as follow

1
2
3
KuroMac:helloworld ziheji$ go run helloworld.go
# command-line-arguments
./helloworld.go:16:1: syntax error: unexpected semicolon or newline before {

Github Repository

The github source code for this note can be found at https://github.com/Kuro1Fury/GolangStudy/tree/main/helloworld

Credit

Thanks for the tutorial 8 小时转职 Golang 工程师(如果你想低成本学习 Go 语言) made by 刘丹冰 Aceld