# Hello World

这是[Golang基础教程](https://sunwenfei.gitbook.io/sunwenfei/golang/golang-ji-chu-jiao-cheng)的第二篇，如果你还未安装Golang，可以参考上一节[安装](https://sunwenfei.gitbook.io/sunwenfei/golang/gonglang-jian-ming-jiao-cheng/jie-shao/an-zhuang)进行安装。

学习编程最好的办法当然就是上手实践，下面我们开始编写第一个Golang程序。我个人推荐使用[Visual Studio Code](https://code.visualstudio.com/)来编写代码，搭配上[go插件](https://marketplace.visualstudio.com/items?itemName=ms-vscode.Go)可以使用代码自动补全等非常不错的特性。

#### 创建工作目录

在开始写代码之前，我们需要先创建工作目录。

如果你使用的是**MAC**或者**Linux**操作系统，在你的系统根目录下创建`go`文件夹：`$HOME/go`；如果你使用的是`Windows`操作系统，那么在目录`C:\Users\YourName`下面创建`go`文件夹：`C:\Users\YourName\go`。

其实我们本可以通过环境变量`GOPATH`来自定义工作目录，但是为了简便起见我们暂时就用上面的目录。

Golang所有源代码都必须位于名为`src`的目录内，因此我们需要在前面的`go`目录下面接着创建`src`目录。所有Golang项目必须在`src`目录下面有独立的子目录，因此我们接着创建目录`hello`。都完成之后目录结构应该如下所示：

```
    go
        src
            hello
```

在`hello`项目目录下创建文件`helloworld.go`并把下面的代码粘贴进去：

```go
package main

import "fmt"

func main() {  
    fmt.Println("Hello World")
}
```

目前为止我们的目录结构如下所示：

```
    go
        src
            hello
                helloworld.go
```

#### 运行Golang程序

运行Golang程序有多种方式，我们一个一个看。

1.使用`go run`命令 在命令行界面输入如下命令：

```
go run workspacepath/src/hello/helloworld.go
```

这里的`workspacepath`应该替换为你自己的Golang工程根目录，Windows下面是`C:/Users/YourName/go`，Mac和Linux系统应该是`$HOME/go`。

正常的话，运行完在控制台会打印输出`Hello World`。

2.使用`go install`命令； 首先执行`go install hello`，然后执行`workspacepath/bin/hello`运行程序。

这里的`workspacepath`应该替换为你自己的Golang工程根目录，Windows下面是`C:/Users/YourName/go`，Mac和Linux系统应该是`$HOME/go`。

正常的话，运行完在控制台会打印输出`Hello World`。

当你执行`go install hello`时，Golang会在工作根目录下面查找`hello`包，找到后会进行编译，在`bin`目录下生成可执行二进制程序`hello`(Windows系统下是`hello.ext`)，执行完`go install hello`之后的目录结构如下所示：

```
    go
        bin
            hello
        src
            hello
                helloworld.go
```

3.使用Go Playground 我们可以在线运行一些比较简单的Golang程序。[点击这里](https://play.golang.org/p/VtXafkQHYe)可以在线运行前面的Hello World程序。

当然，你也可以使用Go Playground来把你的代码片段分享给其他人。

#### Hello World程序解释说明

我们前面键入的Hello World程序如下：

```go
package main //1

import "fmt" //2

func main() { //3  
    fmt.Println("Hello World") //4
}
```

这里我们简要介绍下每行代码是什么意思，后面章节会详细说明。

`package main` 每个Golang文件都由语句`package name`开始，与`Node.JS`等其他编程语言相似，Golang通过`包`来进行代码划分和代码复用。这里我们的包名称为`main`。

`import "fmt"` 这里引入了`fmt`包，用来打印内容到标准输出。

`function main()` `main`函数非常特殊，是整个程序的执行入口。`main函数`**总是位于名为**`main`**的包中。**`{`和`}`一对打括号表示`main`函数的开始与结束。

`fmt.Println("Hello World")` `fmt`包的`Println`函数用来输出内容到标准输出。

本节的代码可以从[github](https://github.com/golangbot/hello)下载。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sunwenfei.gitbook.io/sunwenfei/golang/golang-ji-chu-jiao-cheng/jie-shao/hello-world.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
