Go is a compiled, statically-typed programming language created by Google in 2009. It is designed to simplify systems programming, with a focus on speed, performance, and code readability. Go includes built-in garbage collection, easy-to-use concurrency via goroutines, and fast compilation times.
Go là ngôn ngữ lập trình được biên dịch, statically-typed, được tạo bởi Google vào năm 2009. Go được thiết kế để đơn giản hóa lập trình hệ thống, với tập trung vào tốc độ, hiệu suất và dễ đọc của code. Go có garbage collection tích hợp, hỗ trợ đa luồng (concurrency) dễ sử dụng thông qua goroutines, và thời gian biên dịch nhanh.
Go is used by major companies like Docker, Kubernetes, Cloudflare, and Uber. It is popular for backend services, command-line tools, and network applications. Unlike TypeScript (a dynamically-typed language that compiles to JavaScript), Go is a compiled language with a strong type system.
Go được sử dụng bởi nhiều công ty lớn như Docker, Kubernetes, Cloudflare và Uber. Nó được ưa chuộng cho các dịch vụ backend, công cụ dòng lệnh, và các ứng dụng mạng. Khác với TypeScript (ngôn ngữ động được biên dịch sang JavaScript), Go là ngôn ngữ biên dịch với hệ thống kiểu mạnh mẽ.
To install Go, download the official distribution from golang.org. The distribution includes the compiler (go), package management tools, and the standard library. After installation, verify it by checking the version.
Để cài đặt Go, hãy tải xuống bộ phân phối chính thức từ golang.org. Bộ phân phối bao gồm trình biên dịch (go), công cụ quản lý gói, và thư viện tiêu chuẩn. Sau khi cài đặt, xác minh bằng cách kiểm tra phiên bản.
1# Download and install from https://golang.org/dl23# Verify installation (run in a new terminal)4go version56# Example output:7# go version go1.21.0 linux/amd64Go uses the GOPATH environment variable to define the workspace directory (usually ~/go). However, from Go 1.11 onwards, module mode is the recommended approach — GOPATH is no longer required. Packages are managed via go.mod.
Go sử dụng biến môi trường GOPATH để xác định thư mục workspace (thường là ~/go). Tuy nhiên, từ Go 1.11 trở đi, module mode là cách được khuyến nghị — GOPATH không còn bắt buộc. Các gói được quản lý thông qua go.mod.
Create a new directory for your project. In Go, the simplest approach is to create a .go file with a main() function. No special setup tools are needed.
Tạo một thư mục mới cho dự án của bạn. Trong Go, cách đơn giản nhất là tạo một file .go với hàm main(). Không cần công cụ đặc biệt để thiết lập dự án.
1# Create a project directory2mkdir hello3cd hello45# Create your first Go file6cat > hello.go << 'EOF'7package main89import "fmt"1011func main() {12 fmt.Println("Hello, World!")13}14EOFExplanation:
Giải thích mã:
- package main: All Go files must belong to a package. The main package is special — it contains the program entry point.
package main: Tất cả file Go phải thuộc một gói (package). Gói main là gói đặc biệt — nó chứa điểm vào chương trình.
- import "fmt": Imports the fmt (format) package from the standard library, allowing you to print output.
import "fmt": Nhập gói fmt (định dạng) từ thư viện tiêu chuẩn, cho phép in ra output.
- func main(): The main function is the entry point. It takes no parameters and returns nothing.
func main(): Hàm main là điểm vào của chương trình. Nó không nhận tham số và không trả về giá trị.
- fmt.Println(): Prints a line of text to stdout with a trailing newline.
fmt.Println(): In một dòng văn bản ra stdout kèm theo ký tự xuống dòng.
Go provides basic commands to run and build programs. go run compiles and runs directly. go build creates an executable binary.
Go cung cấp các lệnh cơ bản để chạy và xây dựng chương trình. go run biên dịch và chạy trực tiếp. go build tạo ra một binary có thể thực thi được.
1# Run the program directly (compile and execute in one step)2go run hello.go34# Build an executable binary (creates 'hello' or 'hello.exe' on Windows)5go build67# Run the binary8./helloTo organize dependencies, create a Go module with go mod init. The module name usually follows the convention example.com/myapp.
Để tổ chức dependencies, hãy tạo một module Go bằng go mod init. Tên module thường theo định dạng example.com/myapp.
1# Initialize a module2go mod init example.com/hello34# This creates a go.mod file:5# module example.com/hello6# go 1.217# (no dependencies initially)The go.mod file defines your module and lists all external dependencies. It is similar to package.json in Node.js but simpler.
File go.mod định nghĩa module của bạn và liệt kê tất cả các dependency bên ngoài. Nó tương tự như package.json trong Node.js nhưng đơn giản hơn.
1module example.com/hello23go 1.2145require (6 github.com/some/package v1.2.37)89require (10 github.com/another/package v0.1.0 // indirect11)- module: The module name — a unique identifier for your package.
module: Tên module — định danh duy nhất cho gói của bạn.
- go: The Go version this code is written for (not a hard requirement).
go: Phiên bản Go mà code này được viết cho (không phải là ràng buộc cứng).
- require: Lists external dependencies with specific versions.
require: Liệt kê các dependency bên ngoài với phiên bản cụ thể.
VS Code with the Go extension (golang.go) is the most popular development environment for Go. This extension provides code completion, type information, code navigation, debugging, and real-time diagnostics.
VS Code với extension Go (golang.go) là môi trường phát triển phổ biến nhất cho Go. Extension này cung cấp tính năng code completion, type information, code navigation, debugging, và real-time diagnostics.
- Install VS Code from https://code.visualstudio.com if you do not have it already.
Cài đặt VS Code từ https://code.visualstudio.com nếu bạn chưa có.
- Open Extensions (Ctrl+Shift+X), search for "Go" and install the official extension.
Mở Extensions (Ctrl+Shift+X), tìm kiếm 'Go' và cài đặt extension chính thức.
- The extension will prompt you to install additional Go tools (gopls language server, dlv debugger). Accept the prompts to install them.
Extension sẽ yêu cầu cài đặt các công cụ Go bổ sung (gopls language server, dlv debugger). Chấp nhận lời nhắc để cài đặt chúng.
gopls (Go Language Server) sẽ tự động tải xuống khi bạn mở dự án Go lần đầu. Quá trình này có thể mất vài phút tùy thuộc vào tốc độ kết nối internet.
Key Takeaways
Điểm Chính
- Go is a compiled, statically-typed language created by GoogleGo là ngôn ngữ biên dịch, statically-typed được tạo bởi Google
- go run compiles and runs your program directlygo run biên dịch và chạy chương trình trực tiếp
- go mod init creates a module and go.mod file for dependency managementgo mod init tạo module và file go.mod để quản lý dependency
- Every Go program needs package main with a main() entry point functionMỗi chương trình Go cần package main với hàm entry point main()
Practice
Test your understanding of this chapter
What is the entry point function name in every Go program?
Tên hàm entry point trong mỗi chương trình Go là gì?
Go source files use the .go file extension.
File mã nguồn Go sử dụng phần mở rộng .go.
Complete the command to run a Go program directly
Hoàn thành lệnh để chạy chương trình Go trực tiếp
go hello.go
Which command creates a go.mod file for dependency management?
Lệnh nào tạo file go.mod để quản lý dependency?
In Go, GOPATH is still required for modern module-based projects.
Trong Go, GOPATH vẫn bắt buộc cho các dự án dựa trên module hiện đại.