GO学习 goto语句和随机函数

309阅读模式

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

懂站帝
  • 本文由 发表于 2022年5月10日 22:23:27
  • 版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至395045033@qq.com举报,一经查实,本站将立刻删除。
评论  0  访客  0