学无止境,今天继续学习go语言的基础语法
变量(Variables):
-
变量声明:
govar x int -
变量初始化:
govar x int = 10或者可以使用类型推断:
gox := 10 -
多变量声明:
govar a, b, c int -
同时初始化多个变量:
govar a, b, c = 1, 2, 3 -
全局变量:
在函数外部声明的变量是全局变量。
常量(Constants):
-
常量声明:
goconst pi = 3.14159 -
多常量声明:
goconst ( a = 1 b = 2 c = 3 ) -
枚举常量:
goconst ( Sunday = iota // 0 Monday // 1 Tuesday // 2 Wednesday // 3 Thursday // 4 Friday // 5 Saturday // 6 ) -
自增常量:
goconst ( x = iota * 10 y z ) // x=0, y=10, z=20
这里是一个简单的例子,演示了变量和常量的使用:
go
package main
import "fmt"
func main() {
// 变量
var age int
age = 30
name := "Alice"
// 常量
const pi = 3.14159
fmt.Println("Name:", name)
fmt.Println("Age:", age)
fmt.Println("Pi:", pi)
}
运算符
Go语言支持各种运算符,包括算术运算符、关系运算符、逻辑运算符等。以下是一些常见的Go语言
算术运算符:
+:加法-:减法*:乘法/:除法%:取余
关系运算符:
==:等于!=:不等于<:小于>:大于<=:小于等于>=:大于等于
逻辑运算符:
&&:逻辑与||:逻辑或!:逻辑非
位运算符:
&:按位与|:按位或^:按位异或<<:左移>>:右移
赋值运算符:
=:赋值+=:加并赋值-=:减并赋值*=:乘并赋值/=:除并赋值%=:取余并赋值&=:按位与并赋值|=:按位或并赋值^=:按位异或并赋值<<=:左移并赋值>>=:右移并赋值
其他运算符:
&:取地址*:指针解引用<-:用于通道操作符
示例:
go
package main
import "fmt"
func main() {
// 算术运算符
a := 10
b := 20
fmt.Println("a + b =", a+b)
fmt.Println("a - b =", a-b)
fmt.Println("a * b =", a*b)
fmt.Println("a / b =", a/b)
fmt.Println("a % b =", a%b)
// 关系运算符
fmt.Println("a == b is", a == b)
fmt.Println("a != b is", a != b)
fmt.Println("a < b is", a < b)
fmt.Println("a > b is", a > b)
fmt.Println("a <= b is", a <= b)
fmt.Println("a >= b is", a >= b)
// 逻辑运算符
x := true
y := false
fmt.Println("x && y is", x && y)
fmt.Println("x || y is", x || y)
fmt.Println("!x is", !x)
// 位运算符
fmt.Println("a & b =", a&b)
fmt.Println("a | b =", a|b)
fmt.Println("a ^ b =", a^b)
fmt.Println("a << 1 =", a<<1)
fmt.Println("a >> 1 =", a>>1)
// 赋值运算符
c := 5
c += 3
fmt.Println("c += 3 is", c)
// 其他运算符
pointer := &a
fmt.Println("Address of a:", pointer)
fmt.Println("Value at address:", *pointer)
}
这只是一些常见的运算符,Go语言还有其他一些运算符,如通道操作符 <- 用于发送和接收数据。
Go语言提供了常见的条件语句和循环语句,包括if语句、switch语句、for语句等。
条件语句:
1. if 语句:
go
package main
import "fmt"
func main() {
x := 10
// 基本的 if 语句
if x > 5 {
fmt.Println("x is greater than 5")
}
// if-else 语句
if x > 5 {
fmt.Println("x is greater than 5")
} else {
fmt.Println("x is not greater than 5")
}
// if-else if-else 语句
if x > 5 {
fmt.Println("x is greater than 5")
} else if x < 5 {
fmt.Println("x is less than 5")
} else {
fmt.Println("x is equal to 5")
}
}
2. switch 语句:
go
package main
import "fmt"
func main() {
day := "Monday"
switch day {
case "Monday":
fmt.Println("It's Monday!")
case "Tuesday":
fmt.Println("It's Tuesday!")
case "Wednesday":
fmt.Println("It's Wednesday!")
default:
fmt.Println("It's some other day.")
}
// 使用 switch 表达式
num := 5
switch {
case num > 0:
fmt.Println("Positive")
case num < 0:
fmt.Println("Negative")
default:
fmt.Println("Zero")
}
}
循环语句
1. for 循环:
go
package main
import "fmt"
func main() {
// 基本的 for 循环
for i := 0; i < 5; i++ {
fmt.Println(i)
}
// for 循环用于迭代数组或切片
numbers := []int{1, 2, 3, 4, 5}
for index, value := range numbers {
fmt.Printf("Index: %d, Value: %d\n", index, value)
}
// 无限循环
// for {
// fmt.Println("This will run forever.")
// // 可以使用 break 或 return 语句来退出无限循环
// }
}
2. while 循环:
Go语言中没有专门的 while 关键字,但你可以使用 for 来实现相同的效果:
go
package main
import "fmt"
func main() {
// 模拟 while 循环
i := 0
for i < 5 {
fmt.Println(i)
i++
}
}
3. do-while 循环:
Go语言中也没有 do-while 循环,但你可以使用 for 和 break 来模拟它:
go
package main
import "fmt"
func main() {
// 模拟 do-while 循环
i := 0
for {
fmt.Println(i)
i++
if i >= 5 {
break
}
}
}
这些示例覆盖了Go语言中的条件语句和循环语句。