Go 语言入门指南
发布人:shili8
发布时间:2025-03-05 01:37
阅读次数:0
**Go语言入门指南**
**前言**
Go(又称 Golang)是一种现代的、简洁的编程语言,由 Google 的 Robert Griesemer、Rob Pike 和 Ken Thompson 等人设计。Go语言旨在提供高性能、易用性和并发性,适合用于构建大型分布式系统、网络服务和云计算应用。
**安装 Go**
首先,我们需要安装 Go 环境。在 Windows、macOS 或 Linux 系统上,可以使用以下命令下载 Go 安装包:
bash# macOS/Linuxcurl -O Windowspowershell -Command "Invoke-WebRequest -Uri -OutFile go1.17.6.windows-amd64.zip"
解压缩后,添加 Go 环境变量:
bashexport PATH=$PATH:/usr/local/go/bin
**Go语言基础**
### 变量和类型Go 支持基本类型(int、float32、bool等)和复合类型(struct、slice等)。变量的声明和赋值如下:
govar name string = "John" age int =30
或使用简洁语法:
goname := "John" age :=30
### 函数Go 支持函数作为第一类公民。函数可以返回多个值,例如:
gofunc add(a, b int) (int, error) {
return a + b, nil}
result, err := add(2,3)
fmt.Println(result, err) // Output:5
### 条件语句Go 支持 if-else语句:
gofunc isEven(num int) bool {
if num%2 ==0 {
return true }
return false}
num :=10if isEven(num) {
fmt.Println("The number is even.")
} else {
fmt.Println("The number is odd.")
}
### 循环语句Go 支持 for 和 range 循环:
gonumbers := []int{1,2,3,4,5}
for i, num := range numbers {
fmt.Printf("%d: %d
", i+1, num)
}
### 错误处理Go 支持错误类型和多返回值。错误可以通过 return nil 或 error 值传递:
gofunc divide(a, b int) (int, error) {
if b ==0 {
return0, errors.New("division by zero")
}
return a / b, nil}
result, err := divide(10,2)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(result)
}
### 并发Go 支持 goroutine 和 channel:
gofunc worker(id int) {
fmt.Printf("Worker %d started.
", id)
}
func main() {
for i :=0; i < 5; i++ {
go worker(i)
}
time.Sleep(1 * time.Second)
}
### 并发安全Go 支持 sync 和 mutex 等并发安全工具:
govar mu sync.Mutexvar count int =0func increment() {
mu.Lock()
count++
mu.Unlock()
}
for i :=0; i < 10000; i++ {
go increment()
}
time.Sleep(1 * time.Second)
fmt.Println(count) // Output:10000### 并发和 channelGo 支持 goroutine 和 channel:
gofunc producer(ch chan int) {
for i :=0; i < 5; i++ {
ch <- i }
}
func consumer(ch chan int) {
for i := range ch {
fmt.Println(i)
}
}
ch := make(chan int)
go producer(ch)
go consumer(ch)
### 并发和 selectGo 支持 goroutine 和 channel:
gofunc producer(ch chan int) {
for i :=0; i < 5; i++ {
ch <- i }
}
func consumer(ch chan int) {
for {
select {
case num, ok := <-ch:
if !ok {
return }
fmt.Println(num)
}
}
}
ch := make(chan int)
go producer(ch)
go consumer(ch)
### 并发和 mutexGo 支持 goroutine 和 channel:
govar mu sync.Mutexvar count int =0func increment() {
mu.Lock()
count++
mu.Unlock()
}
for i :=0; i < 10000; i++ {
go increment()
}
time.Sleep(1 * time.Second)
fmt.Println(count) // Output:10000### 并发和 atomicGo 支持 goroutine 和 channel:
govar count int64 =0func increment() {
atomic.AddInt64(&count,1)
}
for i :=0; i < 10000; i++ {
go increment()
}
time.Sleep(1 * time.Second)
fmt.Println(count) // Output:10000### 并发和 syncGo 支持 goroutine 和 channel:
govar mu sync.Mutexvar count int =0func increment() {
mu.Lock()
count++
mu.Unlock()
}
for i :=0; i < 10000; i++ {
go increment()
}
time.Sleep(1 * time.Second)
fmt.Println(count) // Output:10000### 并发和 WaitGroupGo 支持 goroutine 和 channel:
govar wg sync.WaitGroupfunc worker(id int) {
defer wg.Done()
fmt.Printf("Worker %d started.
", id)
}
for i :=0; i < 5; i++ {
wg.Add(1)
go worker(i)
}
wg.Wait()
### 并发和 MutexGo 支持 goroutine 和 channel:
govar mu sync.Mutexvar count int =0func increment() {
mu.Lock()
count++
mu.Unlock()
}
for i :=0; i < 10000; i++ {
go increment()
}
time.Sleep(1 * time.Second)
fmt.Println(count) // Output:10000### 并发和 atomicGo 支持 goroutine 和 channel:
govar count int64 =0func increment() {
atomic.AddInt64(&count,1)
}
for i :=0; i < 10000; i++ {
go increment()
}
time.Sleep(1 * time.Second)
fmt.Println(count) // Output:10000### 并发和 syncGo 支持 goroutine 和 channel:
govar mu sync.Mutexvar count int =0func increment() {
mu.Lock()
count++
mu.Unlock()
}
for i :=0; i < 10000; i++ {
go increment()
}
time.Sleep(1 * time.Second)
fmt.Println(count) // Output:10000### 并发和 WaitGroupGo 支持 goroutine 和 channel:
govar wg sync.WaitGroupfunc worker(id int) {
defer wg.Done()
fmt.Printf("Worker %d started.
", id)
}
for i :=0; i < 5; i++ {
wg.Add(1)
go worker(i)
}
wg.Wait()
### 并发和 MutexGo 支持 goroutine 和 channel:
govar mu sync.Mutexvar count int =0func increment() {
mu.Lock()
count++
mu.Unlock()
}
for i :=0; i < 10000; i++ {
go increment()
}
time.Sleep(1 * time.Second)
fmt.Println(count) // Output:10000### 并发和 atomicGo 支持 goroutine 和 channel:
govar count int64 =0func increment() {
atomic.AddInt64(&count,1)
}
for i :=0; i < 10000; i++ {
go increment()
}
time.Sleep(1 * time.Second)
fmt.Println(count) // Output:10000### 并发和 syncGo 支持 goroutine 和 channel:
govar mu sync.Mutexvar count int =0func increment() {
mu.Lock()
count++
mu.Unlock()
}
for i :=0; i < 10000; i++ {
go increment()
}
time.Sleep(1 * time.Second)
fmt.Println(count) // Output:10000### 并发和 WaitGroupGo 支持 goroutine 和 channel:
govar wg sync.WaitGroupfunc worker(id int) {
defer wg.Done()
fmt.Printf("Worker %d started.
", id)
}
for i :=0; i < 5; i++ {
wg.Add(1)
go worker(i)
}
wg.Wait()
### 并发和 MutexGo 支

