为了督促并记录自己关于GO语言的学习,从现在开始会记录下我学习GO语言的历程。
下面呢是习题2.1和2.2的练习题:
2.1、tempconv包下的tempconv.go:
```go
package tempconv
type K float32
type C float32
func KToC(k K) C {
return C(-273.15 + k)
}
func CToK(c C) K {
return K(c + 273.15)
}
```
在practice_0201文件下的main.go里引入tempconv包并调用输出:
```go
package main
import (
"fmt"
"tempconv"
)
func main() {
// fmt.Println("Hello World!")
var k tempconv.K = 1
c := tempconv.KToC(k)
fmt.Println(c)
var c0 tempconv.C = 1
k0 := tempconv.CToK(c0)
fmt.Println(k0)
}
```
2.2、
```go
// practice_0202 project main.go
package main
import (
"fmt"
)
type Transf struct {
Celsius float32
Fahrenheit float32
Inch float32
M float32
Pound float32
KG float32
}
func main() {
transform(20)
}
func transform(n float32) {
var trans Transf
trans.Celsius = -273.15 + n
trans.Fahrenheit = n + 273.15
trans.Inch = n * 0.0254
trans.M = n / 0.0254
trans.Pound = 0.4535924 * n
trans.KG = n / 0.4535924
fmt.Println(n, "Fahrenheit=", trans.Celsius, "Celsius")
fmt.Println(n, "Celsius=", trans.Fahrenheit, "Fahrenheit")
fmt.Println(n, "Inch=", trans.M, "M")
fmt.Println(n, "M=", trans.Inch, "Inch")
fmt.Println(n, "Pound=", trans.KG, "KG")
fmt.Println(n, "KG=", trans.Pound, "Pound")
}
```
有疑问加站长微信联系(非本文作者))