Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f383162

Browse files
committed
update
1 parent 6743d65 commit f383162

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package tempconv
2+
3+
type Celsius float64
4+
type Fahrenheit float64
5+
6+
const (
7+
AbsoluteZeroC Celsius = -273.15
8+
FreezingC Celsius = 0
9+
BoilingC Celsius = 100
10+
)
11+
12+
func CtoF(c Celsius) Fahrenheit { return Fahrenheit(c*9/5 + 32) }
13+
func FToC(f Fahrenheit) Celsius { return Celsius((f - 32) * 5 / 9) }
14+
15+
// Celsius 和 Fahrenheit 虽然都是 float64 但是他们不是同一个数据类型

‎ch02 程序结构/README.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,29 @@ v = <-ch // 管道接收,失败是返回零值(阻塞不算失败)
230230
_, ok = m[key] // 丢弃字节数
231231
_, ok = x.(T) // 只检测类型,忽略具体值
232232
```
233+
#### 2.4.2 可赋值性
234+
> 赋值语句是显示的赋值形式,但是程序中还有很多地方会发生隐式的赋值行为:函数调用会隐式的将调用的参数的值赋给函数的参数变量,一个返回语句会隐式的将返回操作的值赋值给结果变量,一个符合类型的字面量也会产生赋值行为。例如:
235+
236+
```golang
237+
medals := []string{"gold", "silver", "bronze"}
238+
// 类似
239+
medals[0] = "gold"
240+
medals[1] = "silver"
241+
medals[2] = "bronze"
242+
```
243+
规则:类型必须完全匹配,`nil` 可以赋值给任何指针或引用类型的变量
244+
245+
值得注意的是,对于两个值是否可以用`==``!=`进行比相等比较的能力也和可赋值能力有关系:对于任何类型的值的相等比较,第二个值必须是对第一个值类型对应的变量是可赋值的,反之亦然。
246+
247+
### 2.5 类型
248+
```golang
249+
gopl.io/ch2/tempconv0
250+
251+
// Package tempconv performs Celsius and Fahrenheit temperature computations. package tempconv
252+
253+
import "fmt" type Celsius float64 // 摄氏温度 type Fahrenheit float64 // 华氏温度
254+
255+
const ( AbsoluteZeroC Celsius = -273.15 // 绝对零度 FreezingC Celsius = 0 // 结冰点温度 BoilingC Celsius = 100 // 沸水温度 )
256+
257+
func CToF(c Celsius) Fahrenheit { return Fahrenheit(c*9/5 + 32) } func FToC(f Fahrenheit) Celsius { return Celsius((f - 32) * 5 / 9) }
258+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /