青海百度关键词seo北京seo外包
学无止境,今天继续学习go语言的基础语法
变量(Variables):
-
变量声明:
var x int
-
变量初始化:
var x int = 10
或者可以使用类型推断:
x := 10
-
多变量声明:
var a, b, c int
-
同时初始化多个变量:
var a, b, c = 1, 2, 3
-
全局变量:
在函数外部声明的变量是全局变量。
常量(Constants):
-
常量声明:
const pi = 3.14159
-
多常量声明:
const (a = 1b = 2c = 3 )
-
枚举常量:
const (Sunday = iota // 0Monday // 1Tuesday // 2Wednesday // 3Thursday // 4Friday // 5Saturday // 6 )
-
自增常量:
const (x = iota * 10yz ) // x=0, y=10, z=20
这里是一个简单的例子,演示了变量和常量的使用:
package mainimport "fmt"func main() {// 变量var age intage = 30name := "Alice"// 常量const pi = 3.14159fmt.Println("Name:", name)fmt.Println("Age:", age)fmt.Println("Pi:", pi)
}
运算符
Go语言支持各种运算符,包括算术运算符、关系运算符、逻辑运算符等。以下是一些常见的Go语言
算术运算符:
+
:加法-
:减法*
:乘法/
:除法%
:取余
关系运算符:
==
:等于!=
:不等于<
:小于>
:大于<=
:小于等于>=
:大于等于
逻辑运算符:
&&
:逻辑与||
:逻辑或!
:逻辑非
位运算符:
&
:按位与|
:按位或^
:按位异或<<
:左移>>
:右移
赋值运算符:
=
:赋值+=
:加并赋值-=
:减并赋值*=
:乘并赋值/=
:除并赋值%=
:取余并赋值&=
:按位与并赋值|=
:按位或并赋值^=
:按位异或并赋值<<=
:左移并赋值>>=
:右移并赋值
其他运算符:
&
:取地址*
:指针解引用<-
:用于通道操作符
示例:
package mainimport "fmt"func main() {// 算术运算符a := 10b := 20fmt.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 := truey := falsefmt.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 := 5c += 3fmt.Println("c += 3 is", c)// 其他运算符pointer := &afmt.Println("Address of a:", pointer)fmt.Println("Value at address:", *pointer)
}
这只是一些常见的运算符,Go语言还有其他一些运算符,如通道操作符 <-
用于发送和接收数据。
Go语言提供了常见的条件语句和循环语句,包括if语句、switch语句、for语句等。
条件语句:
1. if
语句:
package mainimport "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
语句:
package mainimport "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 := 5switch {case num > 0:fmt.Println("Positive")case num < 0:fmt.Println("Negative")default:fmt.Println("Zero")}
}
循环语句
1. for
循环:
package mainimport "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
来实现相同的效果:
package mainimport "fmt"func main() {// 模拟 while 循环i := 0for i < 5 {fmt.Println(i)i++}
}
3. do-while
循环:
Go语言中也没有 do-while
循环,但你可以使用 for
和 break
来模拟它:
package mainimport "fmt"func main() {// 模拟 do-while 循环i := 0for {fmt.Println(i)i++if i >= 5 {break}}
}
这些示例覆盖了Go语言中的条件语句和循环语句。