mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 03:06:41 +08:00
04.5.md
This commit is contained in:
@@ -144,17 +144,20 @@ Example 4.8 [type_mixing.go](examples/chapter_4/type_mixing.go)
|
|||||||
Example 4.9 [casting.go](examples/chapter_4/casting.go)
|
Example 4.9 [casting.go](examples/chapter_4/casting.go)
|
||||||
|
|
||||||
package main
|
package main
|
||||||
import “fmt”
|
|
||||||
func main() {
|
|
||||||
var n int16 = 34
|
|
||||||
var m int32
|
|
||||||
// compiler error: cannot use n (type int16) as type int32 in assignment
|
|
||||||
//m = n
|
|
||||||
m = int32(n)
|
|
||||||
|
|
||||||
fmt.Printf(“32 bit int is: %d\n”, m)
|
import “fmt”
|
||||||
fmt.Printf(“16 bit int is: %d\n”, n)
|
|
||||||
|
func main() {
|
||||||
|
var n int16 = 34
|
||||||
|
var m int32
|
||||||
|
// compiler error: cannot use n (type int16) as type int32 in assignment
|
||||||
|
//m = n
|
||||||
|
m = int32(n)
|
||||||
|
|
||||||
|
fmt.Printf(“32 bit int is: %d\n”, m)
|
||||||
|
fmt.Printf(“16 bit int is: %d\n”, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
// the output is:
|
// the output is:
|
||||||
32 bit int is: 34
|
32 bit int is: 34
|
||||||
16 bit int is: 34
|
16 bit int is: 34
|
||||||
|
Reference in New Issue
Block a user