goto语句
package main
import "fmt"
func main() {
/*
goto语句:
*/
var a = 10
LOOP:
for a < 20{
if a == 15{
a += 1
goto LOOP
}
fmt.Printf("a的值为:%d\n",a)
a++
}
fmt.Println("_____________分隔符_______________")
for i := 0;i< 10;i++{
for j := 0;j<10;j++{
if j ==2{
goto breakHere
}
}
}
return
breakHere:
fmt.Println("done........")
}
运行输出:文章源自懂站帝-http://www.sfdkj.com/12810.html
a的值为:10
a的值为:11
a的值为:12
a的值为:13
a的值为:14
a的值为:16
a的值为:17
a的值为:18
a的值为:19
_____________分隔符_______________
done........
Process finished with exit code 0
随机函数:文章源自懂站帝-http://www.sfdkj.com/12810.html
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
/*
生成随机函数random:
伪随机数,根据一定的算法公式算出来的。
math/rand
*/
//固定随机数
num1 := rand.Int()
fmt.Println(num1)
//固定随机数
for i :=0;i<10;i++{
num := rand.Intn(10)
fmt.Println(num)
}
//固定随机数
rand.Seed(1000)
num2:= rand.Intn(10)
fmt.Println("-->",num2)
t1 := time.Now()
fmt.Println(t1)
fmt.Printf("%T\n",t1)
//时间戳:指定时间,距离1970年1月1日0点0分0秒,指尖的时间插值:秒,纳秒
timesStamp1 :=t1.Unix()
fmt.Println(timesStamp1)
timesStamp2 := t1.UnixNano()
fmt.Println(timesStamp2)
//每次执行获得不同的随机数
//step1:设置种子数,可以设置成时间戳
rand.Seed(time.Now().UnixNano())
for i :=0;i<5;i++{
//step2:调用生成随机数的函数
fmt.Println("-->",rand.Intn(100))
}
/*
[15.76]
[3,48]
[0,45]+3
Intn(n) //[0,n)
*/
num3 := rand.Intn(46)+3//[3,49]
fmt.Println(num3)
num4 := rand.Intn(62)+15//[15,76]
fmt.Println(num4)
}
运行输出:文章源自懂站帝-http://www.sfdkj.com/12810.html
5577006791947779410
7
7
9
1
8
5
0
6
0
4
--> 5
2022-05-10 17:58:37.6571704 +0800 CST m=+0.009994601
time.Time
1652176717
1652176717657170400
--> 77
--> 32
--> 48
--> 75
--> 58
28
58
Process finished with exit code 0
读完点个赞,给我的坚持更新注入新的活力。文章源自懂站帝-http://www.sfdkj.com/12810.html文章源自懂站帝-http://www.sfdkj.com/12810.html