本章学习目标
- 学会让计算机执行数学运算
- 学会声明变量和常量
- 了解声明和赋值的区别
- 学会使用标准库生成伪随机数
例子(2.1)
package main
// 我的体重程序
import (
"fmt"
)
//main是开始函数
func main() {
fmt.Print("My weight in the surface of Mars is ")
fmt.Print(149.0 * 0.3783) //打印 56.3667
fmt.Print(" lbs, and I would be ")
fmt.Print(41 * 365 / 687) // 打印21
fmt.Print(" years old.")
}
/*
执行结果:
My weight in the surface of Mars is 56.3667 lbs, and I would be 21 years old.
*/
例子(2.2)
package main
// 我的体重程序
import (
"fmt"
)
//main是开始函数
func main() {
/*
fmt.Print("My weight in the surface of Mars is ")
fmt.Print(149.0 * 0.3783) //打印 56.3667
fmt.Print(" lbs, and I would be ")
fmt.Print(41 * 365 / 687) // 打印21
fmt.Print(" years old.")
*/
fmt.Println("My weight in the surface of Mars is ", 149.0*0.3783, " lbs, and I would be ", 41*365/687, "years old.")
}
/*
执行结果:
My weight in the surface of Mars is 56.3667 lbs, and I would be 21 years old.
*/
使用fmt.Printf来控制打印的输出结果
与fmt.Print和fmt.Println不同,fit.Printf的第一个参数必须为字符串
这个字符串里面包含了像%v这样的格式化动词,它的值由第二个参数的值所代替。
如果指定多个格式化动词,那么它们的值由后边的参数值按其顺序进行替换
例子(2.3)
package main
import (
"fmt"
)
func main() {
fmt.Printf("My weight in the surface of Mars is %v lbs,", 149.0*0.3783)
fmt.Printf("and I would be %v years old.\n", 41*365/687)
}
/*
执行结果:
My weight in the surface of Mars is 56.3667 lbs,and I would be 21 years old.
*/
使用fmt.Printf对齐文本
在格式化动词里面指定宽度,就可以对齐文本。
正数,向左填充空格。
负数,向右填充空格。
例子(2.4)
package main
import (
"fmt"
)
func main() {
fmt.Printf("%-15v $%4v\n", "SpaceX", 94)
fmt.Printf("%-15v $%4v\n", "Virgin Galactic", 100)
}
/*
执行结果:
SpaceX $ 94
Virgin Galactic $ 100
*/
const,用来声明常量
var,用来声明变量
例子(2.5)
package main
import (
"fmt"
)
func main() {
const lightSpeed = 299792 // km/s
var distance = 56000000 // km
fmt.Println(distance/lightSpeed, "seconds")
distance = 401000000
fmt.Println(distance/lightSpeed, "seconds")
}
/*
执行结果:
186 seconds
1337 seconds
*/
同时声明多个变量
例子(2.6)
package main
import (
"fmt"
)
func main() {
/*
const lightSpeed = 299792 // km/s
var distance = 56000000 // km
fmt.Println(distance/lightSpeed, "seconds")
distance = 401000000
fmt.Println(distance/lightSpeed, "seconds")
*/
var distance = 5000000
var speed = 108000
// 一次声明一组变量
var (
distance = 20000
speed = 22222
)
// 也可以在一行声明多个变量
var distance, speed = 40000, 20000
const a, b = 23, 60
}
赋值操作符
例子(2.7)
package main
import (
"fmt"
)
func main() {
var weight = 149.0
weight = weight * 0.3783 // 把计算后的值再次赋值给变量weight
weight *= 0.3783 // 是上一行的简写
fmt.Println("weight的结果:", weight)
}
/*
执行结果:
weight的结果: 21.32352261
*/
使用rand包,生成伪随机数
例如,Intn函数 可以返回一个指定范围的随机整数。
Import 的路径是 "math/rand"
例子(2.8)
package main
import (
"fmt"
"math/rand"
)
func main() {
var num = rand.Intn(10) + 1 // 生成的范围不包括10 所以+1
fmt.Println(num)
// num = rand.Intn(10) + 1
// fmt.Println(num)
}
/*
执行结果:
2
*/
Malacandra是C.S.Lewis在太空三部曲中给火星起的另外一个名字,编写程序来确认飞船在28天内到达Malacandra的进行速度(公里/小时),假设距离为56000000公里。
package main
import (
"fmt"
)
func main() {
var distance = 56000000
const (
day = 28
hour = 24
)
fmt.Println("结果", distance/(day*hour), "km/h")
}
/*
执行结果:
结果 83333 km/h
*/
原文:https://www.cnblogs.com/walker1024/p/14152493.html