From c3abd6b956d062820889c947a2fd892dbc7d951c Mon Sep 17 00:00:00 2001 From: Charley Zhang Date: Tue, 17 Dec 2013 11:01:29 +0800 Subject: [PATCH] Update 04.4.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 示例代码中的字符串的双引号使用错误 --- eBook/04.4.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/eBook/04.4.md b/eBook/04.4.md index d5b34cf..7295cb3 100644 --- a/eBook/04.4.md +++ b/eBook/04.4.md @@ -55,7 +55,7 @@ var a int = 15 var i = 5 var b bool = false - var str string = “Go says hello to the world!” + var str string = "Go says hello to the world!" 但是 Go 编译器的智商已经高到可以根据变量的值来自动推断其类型,这有点像 Ruby 和 Python 这类动态语言,只不过它们是在运行时进行推断,而 Go 是在编译时就已经完成推断过程。因此,你还可以使用下面的这些形式来声明及初始化变量: @@ -68,7 +68,7 @@ var ( a = 15 b = false - str = “Go says hello to the world!” + str = "Go says hello to the world!" numShips = 50 city string ) @@ -78,9 +78,9 @@ 然而,`var a` 这种语法是不正确的,因为编译器没有任何可以用于自动推断类型的依据。变量的类型也可以在运行时实现自动推断,例如: var ( - HOME = os.Getenv(“HOME”) - USER = os.Getenv(“USER”) - GOROOT = os.Getenv(“GOROOT”) + HOME = os.Getenv("HOME") + USER = os.Getenv("USER") + GOROOT = os.Getenv("GOROOT") ) 这种写法主要用于声明包级别的全局变量,当你在函数体内声明局部变量时,应使用简短声明语法 `:=`,例如:`a := 1`。 @@ -92,15 +92,15 @@ Example 4.5 [goos.go](examples/chapter_4/goos.go) package main import ( - “fmt” - “os” + "fmt" + "os" ) func main() { - var goos string = os.Getenv(“GOOS”) - fmt.Printf(“The operating system is: %s\n”, goos) - path := os.Getenv(“PATH”) - fmt.Printf(“Path is %s\n”, path) + var goos string = os.Getenv("GOOS") + fmt.Printf("The operating system is: %s\n", goos) + path := os.Getenv("PATH") + fmt.Printf("Path is %s\n", path) } 如果你在 Windows 下运行这段代码,则会输出 `The operating system is: windows` 以及相应的环境变量的值;如果你在 Linux 下运行这段代码,则会输出 `The operating system is: linux` 以及相应的的环境变量的值。 @@ -170,7 +170,7 @@ a 和 b 的类型(int 和 bool)将由编译器自动推断。 func main() { var a string = "abc" - fmt.Println(“hello, world”) + fmt.Println("hello, world") } 尝试编译这段代码将得到错误 `a declared and not used`。