当前位置:网站首页>【Go ~ 0到1 】 第一天 6月24 变量,条件判断 循环语句
【Go ~ 0到1 】 第一天 6月24 变量,条件判断 循环语句
2022-06-28 08:16:00 【秋日的晚霞】
1. 变量的声明方式
标准声明
var 变量名 数据类型 = 变量值
简短声明
变量名 := 变量值
2. if 条件判断
1.if 条件判断
//条件判断:
var flag = true
if flag {
fmt.Println("结果为true")
} else {
fmt.Println("结果为false")
}
2.多条件判断
// 多条件判断
age := 30
if age == 20 {
fmt.Println("年龄刚好是20岁")
} else if age > 20 {
fmt.Println("年龄大于20岁")
} else {
fmt.Println("年龄少于20岁")
}
3.作用域判断
num 的作用域仅限于 if语句中
//作用域判断:
if num := 20; num < 30 {
fmt.Println("num值小于30")
} else {
fmt.Println("num值大于30")
}
3.for 循环语句
1.基本格式
for 变量声明;条件判断;步进表达式{
循环体语句
}
for num := 0; num < 100; num++ {
fmt.Printf("从%d到100\n", num)
}
2.死循环
for ;;{
循环体语句
}
for ; ; {
fmt.Println("我是死循环")
}
end. 练习
统计一段文本中 数字,字母 和 汉字的个数
package main
import (
"bufio"
"fmt"
"io"
"os"
"unicode"
)
var (
//数字 字母 汉字
num, wd, han = 0, 0, 0
)
func main() {
//打开文件
file, err := os.Open("D:\\练习文本.txt")
if err != nil {
fmt.Printf("读取文件出现了错误 %v\n", err.Error())
}
//确保关闭io流
defer file.Close()
//获取字符缓冲流
reader := bufio.NewReader(file)
//转换为string
for {
readString, err := reader.ReadString('\n')
if err == io.EOF {
//说明读取完毕
transition(readString)
fmt.Println("文件读取完毕")
break
}
if err != nil {
fmt.Printf("读取字符流出错 %v\n", err)
}
transition(readString)
}
fmt.Println("数字个数 : ", num, " 字母个数 : ", wd, " 汉字个数 :", han)
}
func transition(readString string) {
for a1, str := range readString {
a1 = a1
fmt.Println(str)
if unicode.IsNumber(str) {
num++
}
if unicode.IsLower(str) {
wd++
}
if unicode.IsUpper(str) {
wd++
}
if unicode.Is(unicode.Han, str) {
han++
}
}
}
边栏推荐
- MySQL single table access method
- Redis cerebral fissure
- 你了解TCP协议吗(一)?
- 【学习笔记】最短路 +生成树
- 探讨gis三维系统在矿山行业中的应用
- Leetcode摆动序列系列
- MySQL implements transaction persistence using redo logs
- Children's unit of 2022 Paris fashion week ended successfully at Wuhan station on June 19
- 【学习笔记】差分约束
- Why MySQL cannot insert Chinese data in CMD
猜你喜欢

微内核Zephyr获众多厂家支持!

Uvcgan: unt vision transformer cycle-consistent Gan for unpropared image-to-image translation

Solution: selenium common. exceptions. WebDriverException: Message: ‘chromedriver‘ execu

城联优品向英德捐赠抗洪救灾爱心物资

ROS 笔记(09)— 参数的查询和设置

Activity隐式跳转

MySQL row format parsing

B_QuRT_User_Guide(28)

Vagrant installation

B_ QuRT_ User_ Guide(28)
随机推荐
Priority of JS operator
Solution: selenium common. exceptions. WebDriverException: Message: ‘chromedriver‘ execu
Eslint syntax monitoring off
Not so Mobile
Oracle view tablespace usage
NLP sequence can completely simulate human brain intelligence
Prometheus + grafana + MySQL master-slave replication + host monitoring
Prometheus deployment alarm docking QQ mailbox
Little artist huangxinyang was invited to participate in the Wuhan station of children's unit of Paris Fashion Week
B_QuRT_User_Guide(29)
Oracle view all tablespaces in the current library
Kubernetes notes and the latest k3s installation introduction
Three step problem of leetcode
Installing mysql5.7 under Windows
Estimation of SQL execution cost by MySQL query optimizer
三角变换公式
SQL Master slave Replication Build
Activity implicit jump
Is it reliable to open an account by digging money? Is it safe?
Redis uses sentinel master-slave switching. What should the program do?