django做待办事项网站,图片在线处理工具,手机与电脑网站制作,乾县住房和城乡建设局网站学无止境#xff0c;今天继续学习go语言的基础语法
变量#xff08;Variables#xff09;: 变量声明#xff1a; var x int变量初始化#xff1a; var x int 10或者可以使用类型推断#xff1a; x : 10多变量声明#xff1a; var a, b, c int同时初始化多个变量#…学无止境今天继续学习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
)
// x0, y10, z20这里是一个简单的例子演示了变量和常量的使用
package mainimport fmtfunc 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 fmtfunc main() {// 算术运算符a : 10b : 20fmt.Println(a b , ab)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 , ab)fmt.Println(a | b , a|b)fmt.Println(a ^ b , a^b)fmt.Println(a 1 , a1)fmt.Println(a 1 , a1)// 赋值运算符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 fmtfunc 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 fmtfunc main() {day : Mondayswitch day {case Monday:fmt.Println(Its Monday!)case Tuesday:fmt.Println(Its Tuesday!)case Wednesday:fmt.Println(Its Wednesday!)default:fmt.Println(Its 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 fmtfunc 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 fmtfunc main() {// 模拟 while 循环i : 0for i 5 {fmt.Println(i)i}
}3. do-while 循环:
Go语言中也没有 do-while 循环但你可以使用 for 和 break 来模拟它
package mainimport fmtfunc main() {// 模拟 do-while 循环i : 0for {fmt.Println(i)iif i 5 {break}}
}这些示例覆盖了Go语言中的条件语句和循环语句。