Quick Start
Preface
This article helps you get a general understanding of Go, serving as a starting point to gradually familiarize yourself with Go programming.
Environment Setup
Before writing Go programs, you need to set up the development environment:
-
Install Go:
- Visit the Go official website, download the installation package suitable for your operating system, and install it.
- After installation, run
go version
in the command line to confirm the installation was successful.
-
Configure Workspace:
- The Go development environment requires a workspace, which typically contains three folders:
src
(source code),pkg
(compiled packages), andbin
(executable files). - Set the environment variable
GOPATH
to point to your workspace.
- The Go development environment requires a workspace, which typically contains three folders:
-
Choose Development Tools:
- t is recommended to use IDEs like
VS Code
orGoLand
, which support Go and provide features like code completion, syntax highlighting, and debugging to enhance development efficiency.
- t is recommended to use IDEs like
Basic Syntax
Let's start with the simplest Go program:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
This code demonstrates the basic structure of a Go program:
- package main:Every Go program starts with a package, and the main package is the entry point.
- import "fmt":The fmt package from the standard library is imported to handle formatted output.
- func main():The
main
function is the entry point where program execution begins.
You can run this program by executing go run filename.go
.
Variables and Data Types
Go is a statically typed language, meaning variables need to be declared before use. Go supports various data types, including basic types (integers, floating-point numbers, booleans, strings) and complex types (arrays, slices, structs, pointers, etc.).
var name string = "Go"
age := 10
fmt.Println("Language:", name)
fmt.Println("Age:", age)
In this code, := is a shorthand for variable declaration in Go.
Control Structures
Go provides common control structures like if-else statements, for loops, and switch-case statements.
if age > 10 {
fmt.Println("Go is older than 10 years")
} else {
fmt.Println("Go is a youngster")
}
for i := 0; i < 5; i++ {
fmt.Println("Iteration:", i)
}
for
is the only loop structure in Go, but with different syntax, it can function similarly to a while loop.
Functions
Functions are a core concept in Go. It supports multiple return values, and functions are first-class objects that can be passed as parameters.
func add(a int, b int) int {
return a + b
}
func main() {
sum := add(3, 4)
fmt.Println("Sum:", sum)
}
Concurrent Programming
Concurrency in Go is achieved using Goroutines, lightweight threads managed by the Go runtime. Goroutines are easily created using the go
keyword.
func say(message string) {
fmt.Println(message)
}
func main() {
go say("Hello")
go say("World")
time.Sleep(time.Second) // 等待Goroutines执行完成
}
In this example, the say
function is executed in new Goroutines using the go
keyword. time.Sleep
is used to wait for the Goroutines to complete.
Standard Library and Third-Party Libraries
Go's standard library is powerful enough to meet most development needs. However, when necessary, you can install and use third-party libraries with the go get
command.
go get github.com/some/package
Compilation and Deployment
Go's compilation speed is very fast. The following command generates a cross-platform executable file:
go build filename.go
The generated executable does not depend on any external environment and can be directly deployed on the target machine.
Conclusion
This article introduces the basics of the Go language and common development practices. As a modern programming language, Go has a gentle learning curve and powerful features, making it suitable for various development tasks. I hope this article helps you get started with Go programming and encourages you to further explore and practice through real projects.
Copyright Notice: Free to Share - Non-commercial - No Derivatives - Keep Author - Keep Source
Author: afxcn
Source: https://gostartkit.com/docs/golang/getting-started
Date: September 4, 2024