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 919f1e3

Browse files
committed
包和文件
1 parent abeff12 commit 919f1e3

File tree

1 file changed

+69
-3
lines changed

1 file changed

+69
-3
lines changed

‎ch02 程序结构/README.md‎

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,75 @@ gopl.io/ch2/tempconv0
250250

251251
// Package tempconv performs Celsius and Fahrenheit temperature computations. package tempconv
252252

253-
import "fmt" typeCelsiusfloat64// 摄氏温度 type Fahrenheit float64 // 华氏温度
253+
import "fmt"
254254

255-
const ( AbsoluteZeroC Celsius = -273.15 // 绝对零度 FreezingC Celsius = 0 // 结冰点温度 BoilingC Celsius = 100 // 沸水温度 )
255+
type Celsius float64 // 摄氏温度
256+
type Fahrenheit float64 // 华氏温度
256257

257-
func CToF(c Celsius) Fahrenheit { return Fahrenheit(c*9/5 + 32) } func FToC(f Fahrenheit) Celsius { return Celsius((f - 32) * 5 / 9) }
258+
const (
259+
AbsoluteZeroC Celsius = -273.15 // 绝对零度
260+
FreezingC Celsius = 0 // 结冰点温度
261+
BoilingC Celsius = 100 // 沸水温度
262+
)
263+
264+
func CToF(c Celsius) Fahrenheit { return Fahrenheit(c*9/5 + 32) }
265+
func FToC(f Fahrenheit) Celsius { return Celsius((f - 32) * 5 / 9) }
266+
```
267+
268+
比较运算符`==``<`也可以用来比较一个命名类型的变量和另一个有相同类型的变量,或有着相同底层类型的未命名类型的值之间做比较。但是如果两个值有不同的类型,则不能直接进行比较:
269+
```golang
270+
var c Celsius
271+
var f Fahrenheit
272+
fmt.Println(c == 0) // "true"
273+
fmt.Println(f >= 0) // "true"
274+
fmt.Println(c == f) // compile error: type mismatch
275+
fmt.Println(c == Celsius(f))// "true"
276+
// Celsius(f)是类型转换操作,并不会改变值,仅仅是改变值的类型而已
277+
```
278+
279+
```golang
280+
func (c Celsius) String() string { return fmt.Sprintf("%g°C", c) }
281+
// 许多类型都会定义一个String方法,因为当使用fmt包的打印方法时,将会优先使用该类型对应的String方法返回的结果打印(作者注,这里和 Python 一样,类型有内置函数,可以重写)
282+
```
283+
284+
```golang
285+
c := FToC(212.0)
286+
fmt.Println(c.String()) // "100°C"
287+
fmt.Printf("%v\n", c) // "100°C"; no need to call String explicitly
288+
fmt.Printf("%s\n", c) // "100°C"
289+
fmt.Println(c) // "100°C"
290+
fmt.Printf("%g\n", c) // "100"; does not call String
291+
fmt.Printf(float64(c)) // "100"; does not call String
292+
```
293+
294+
### 2.6 包和文件
295+
296+
每个包都对应一个独立的名字空间。例如:在image包中的Decode函数和在unicode/utf16包中的Decode函数时不同的,要在外部引用该函数,必须显式的使用image.Decode或utf16.Decode形式访问。
297+
298+
在Go语言中,一个简单的规则是:如果一个名字是大写字母开头的,那么该名字是导出的。
299+
300+
```golang
301+
package main
302+
303+
import (
304+
"fmt"
305+
"os"
306+
"strconv"
307+
308+
"gopl.io/ch2/tempconv"
309+
)
310+
311+
func main() {
312+
for _, arg := range os.Args[1:] {
313+
t, err := strconv.ParseFloat(arg, 64)
314+
if err != nil {
315+
fmt.Fprintf(os.Stderr, "cf: %v\n", err)
316+
os.Exit(1)
317+
}
318+
f := tempconv.Fahrenheit(t)
319+
c := tempconv.Celsius(t)
320+
fmt.Printf("%s = %s, %s = %s\n",
321+
f, tempvonc.FToC(f), c, tempconv.CToF(c))
322+
}
323+
}
258324
```

0 commit comments

Comments
(0)

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