当前位置:网站首页>【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++
}
}
}
边栏推荐
- Force buckle 1884 Egg drop - two eggs
- The preliminary round of the sixth season of 2022 perfect children's model Foshan competition area came to a successful conclusion
- 887. egg drop
- 找合适的PMP机构只需2步搞定,一查二问
- Resolution of Rac grid failure to start after server restart
- Discussion on the application of GIS 3D system in mining industry
- Eslint 语法监测关闭
- NLP sequence can completely simulate human brain intelligence
- NPM clean cache
- Devops foundation chapter Jenkins deployment (II)
猜你喜欢

Three step problem of leetcode

Eslint 语法监测关闭

B_QuRT_User_Guide(28)

MySQL two table connection principle (understand join buf)

Trigonometric transformation formula

Unity 获取当前物体正前方,一定角度、距离的坐标点

Eslint syntax monitoring off

Introduction to Devops Basics

ROS notes (08) - definition and use of service data

Almost Union-Find(带权并查集)
随机推荐
Devops foundation chapter Jenkins deployment (II)
RAC enable archive log
匿名页的反向映射
[learning notes] linear basis
你了解TCP协议吗(一)?
MySQL single table access method
[JS] - [DFS, BFS application] - learning notes
微内核Zephyr获众多厂家支持!
ROS 笔记(09)— 参数的查询和设置
解决npm ERR! Unexpected end of JSON input while parsing near问题
In flood fighting and disaster relief, the city donated 100000 yuan of love materials to help Yingde
Is it reliable for flush to register and open an account? Is it safe?
Discussion on the application of GIS 3D system in mining industry
Trigonometric transformation formula
新唐NUC980使用记录:自制开发板(基于NUC980DK61YC)
How to choose an account opening broker? Is it safe to open an account online?
Jacobian matrix J commonly used in slam
Kubernetes notes and the latest k3s installation introduction
Solve NPM err! Unexpected end of JSON input while parsing near
【学习笔记】搜索