Skip to content

Commit

Permalink
docs: supplement do not use hz description in getting-started (#747)
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoranz758 committed Aug 4, 2023
1 parent 9828ea0 commit b19225d
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 105 deletions.
129 changes: 74 additions & 55 deletions content/en/docs/hertz/getting-started/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,11 @@ description: >
## Quick Start

After you have prepared the Golang environment, this chapter will help you to get familiar with Hertz in a very short time.
After completing the environment preparation, you can quickly start the Hertz Server as follows:

### Install the commend tool of hz

First, you need to install the commend tool hz which is used in this chapter

1. Confirm the `GOPATH` environment has been defined correctly (For example `export GOPATH=~/go`)
and the `$GOPATH/bin` has been added to `PATH` environment (For example `export PATH=$GOPATH/bin:$PATH`);
Attention, do not set `GOPATH` to a directory that the current user does not have read/write access to.
2. Install hz: `go install github.com/cloudwego/hertz/cmd/hz@latest`

For more information on how to use hz, please refer to: [hz](https://www.cloudwego.io/zh/docs/hertz/tutorials/toolkit/usage/)

### Determine Where to Store Your Code

1. If your codes are located in `$GOPATH/src`, you will need to create an additional dictionary in `$GOPATH/src` and retrieve your code from that dictionary.

```bash
mkdir -p $(go env GOPATH)/src/github.com/cloudwego
cd $(go env GOPATH)/src/github.com/cloudwego
```

2. If your codes are not placed under `GOPATH`, you can retrieve them directly.

### Generate/Complete the Sample Code

1. Create the hertz_demo folder in the current directory and go to that directory
2. Generate code `hz new`. If your codes are not placed under `GOPATH`, you need to refer [here](https://www.cloudwego.io/docs/hertz/tutorials/toolkit/usage/usage/) and add `-module` (or `-mod`) flag to name your custom module.
3. Tidy & get dependencies

```bash
go mod init # If your codes are not placed under `GOPATH`, you can skip `go mod init`.
go mod tidy
```

If you are currently using a Windows system, you can write the following sample code.

1. Create the hertz_demo folder in the current directory and go to that directory
2. Create the `main.go` file
3. Add the following code to the `main.go` file
1. Create the hertz_demo folder in the current directory and go to that directory.
2. Create the `main.go` file.
3. Add the following code to the `main.go` file.

```go
package main
Expand All @@ -79,54 +44,108 @@ If you are currently using a Windows system, you can write the following sample
}
```

4. Generate the `go.mod` file
4. Generate the `go.mod` file.

```bash
go mod init hertz_demo
```

5. Tidy & get dependencies
5. Tidy & get dependencies.

```bash
go mod tidy
```

6. Run the Sample Code

```bash
go run hertz_demo
```

If the server is launched successfully, you will see following message:

```bash
2022/05/17 21:47:09.626332 engine.go:567: [Debug] HERTZ: Method=GET absolutePath=/ping --> handlerName=main.main.func1 (num=2 handlers)
2022/05/17 21:47:09.629874 transport.go:84: [Info] HERTZ: HTTP server listening on address=[::]:8888
```

Then, we can test the interface:

```bash
curl http://127.0.0.1:8888/ping
```

If nothing goes wrong, we can see the following output:

```bash
{"message":"pong"}
```

## Code Generation Tool hz

Hz is a command-line tool provided by the Hertz framework for generating code, which can be used to generate scaffolding for Hertz projects.

### Install the command tool of hz

First, you need to install the command tool hz which is used in this chapter:

1. Confirm the `GOPATH` environment has been defined correctly (For example `export GOPATH=~/go`)
and the `$GOPATH/bin` has been added to `PATH` environment (For example `export PATH=$GOPATH/bin:$PATH`);
Attention, do not set `GOPATH` to a directory that the current user does not have read/write access to.
2. Install hz: `go install github.com/cloudwego/hertz/cmd/hz@latest`.

For more information on how to use hz, please refer to: [hz](https://www.cloudwego.io/zh/docs/hertz/tutorials/toolkit/usage/).

### Determine Where to Store Your Code

1. If your codes are located in `$GOPATH/src`, you will need to create an additional dictionary in `$GOPATH/src` and retrieve your code from that dictionary.

```bash
mkdir -p $(go env GOPATH)/src/github.com/cloudwego
cd $(go env GOPATH)/src/github.com/cloudwego
```

2. If your codes are not placed under `GOPATH`, you can retrieve them directly.

### Generate/Complete the Sample Code

1. Create the hertz_demo folder in the current directory and go to that directory.
2. Generate code `hz new`. If your codes are not placed under `GOPATH`, you need to refer [here](https://www.cloudwego.io/docs/hertz/tutorials/toolkit/usage/usage/) and add `-module` (or `-mod`) flag to name your custom module.
3. Tidy & get dependencies.

```bash
go mod init # If your codes are not placed under `GOPATH`, you can skip `go mod init`.
go mod tidy
```

### Run the Sample Code

After you have completed the previous steps, you are able to compile & launch the server
After you have completed the previous steps, you are able to compile & launch the server.

```bash
go build -o hertz_demo && ./hertz_demo
```

If the server is launched successfully, you will see following message
If the server is launched successfully, you will see following message:

```bash
2022/05/17 21:47:09.626332 engine.go:567: [Debug] HERTZ: Method=GET absolutePath=/ping --> handlerName=main.main.func1 (num=2 handlers)
2022/05/17 21:47:09.629874 transport.go:84: [Info] HERTZ: HTTP server listening on address=[::]:8888
```

Then, we can test the interface
Then, we can test the interface:

```bash
curl http://127.0.0.1:8888/ping
```

If nothing goes wrong, we can see the following output
If nothing goes wrong, we can see the following output:

```bash
{"message":"pong"}
```

You have now successfully launched Hertz Server successfully and completed an API call. More API examples can be found at [API Examples](https://pkg.go.dev/github.com/cloudwego/hertz).

As for the layout of the Project Dictionary, here is a sample project layout that you can refer to. You can also organise the layout according to your business scenario.

## Directory Structure

As for the project directory structure, you may check [Project Layout](https://github.com/golang-standards/project-layout) for reference,
it can be organized according to the actual situation of the business logic.

You have now successfully launched Hertz Server successfully and completed an API call.
## More examples

Please refer:[hertz-examples](https://github.com/cloudwego/hertz-examples)
Please refer:[Example code](/docs/hertz/tutorials/example/)
123 changes: 73 additions & 50 deletions content/zh/docs/hertz/getting-started/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,15 @@ description: >
2. 推荐使用最新版本的 Golang,或保证现有 Golang 版本 >= 1.15。小于 1.15 版本,可以自行尝试使用但不保障兼容性和稳定性。
3. 确保打开 go mod 支持 (Golang >= 1.15时,默认开启)。

> 目前,Hertz 支持 Linux、macOS、Windows 系统
> 目前,Hertz 支持 Linux、macOS、Windows 系统
## 快速上手

在完成环境准备后,本章节将帮助你快速上手 Hertz
在完成环境准备后,可以按照如下操作快速启动 Hertz Server:

### 安装命令行工具 hz

首先,我们需要安装使用本示例所需要的命令行工具 hz:

1. 确保 `GOPATH` 环境变量已经被正确地定义(例如 `export GOPATH=~/go`)并且将 `$GOPATH/bin` 添加到 `PATH` 环境变量之中(例如 `export PATH=$GOPATH/bin:$PATH`);请勿将 `GOPATH` 设置为当前用户没有读写权限的目录
2. 安装 hz:`go install github.com/cloudwego/hertz/cmd/hz@latest`

更多 hz 使用方法可参考: [hz](https://www.cloudwego.io/zh/docs/hertz/tutorials/toolkit/)

### 确定代码放置位置

1. 若将代码放置于 `$GOPATH/src` 下,需在 `$GOPATH/src` 下创建额外目录,进入该目录后再获取代码:

```bash
mkdir -p $(go env GOPATH)/src/github.com/cloudwego
cd $(go env GOPATH)/src/github.com/cloudwego
```

2. 若将代码放置于 `GOPATH` 之外,可直接获取

### 生成/编写示例代码

1. 在当前目录下创建 hertz_demo 文件夹,进入该目录中
2. 生成代码 `hz new`,若当前不在 `GOPATH`,需要添加 `-module` 或者 `-mod` flag 指定一个自定义的模块名称。详细参考[这里](https://www.cloudwego.io/zh/docs/hertz/tutorials/toolkit/usage/usage/)
3. 整理 & 拉取依赖

```bash
go mod init # 当前目录不在 GOPATH 下不需要 `go mod init` 这一步
go mod tidy
```

如果当前使用的是 Windows 环境,可以编写如下的示例代码:

1. 在当前目录下创建 hertz_demo 文件夹,进入该目录中
2. 创建 `main.go` 文件
3.`main.go` 文件中添加以下代码
1. 在当前目录下创建 hertz_demo 文件夹,进入该目录中。
2. 创建 `main.go` 文件。
3.`main.go` 文件中添加以下代码。

```go
package main
Expand All @@ -78,51 +45,107 @@ description: >
}
```

4. 生成 `go.mod` 文件
4. 生成 `go.mod` 文件

```bash
go mod init hertz_demo
```

5. 整理 & 拉取依赖
5. 整理 & 拉取依赖。

```bash
go mod tidy
```

6. 运行示例代码。

```bash
go run hertz_demo
```

如果成功启动,你将看到以下信息:

```bash
2022/05/17 21:47:09.626332 engine.go:567: [Debug] HERTZ: Method=GET absolutePath=/ping --> handlerName=main.main.func1 (num=2 handlers)
2022/05/17 21:47:09.629874 transport.go:84: [Info] HERTZ: HTTP server listening on address=[::]:8888
```

接下来,我们可以对接口进行测试:

```bash
curl http://127.0.0.1:8888/ping
```

如果不出意外,我们可以看到类似如下输出:

```bash
{"message":"pong"}
```

## 代码自动生成工具 hz

hz 是 Hertz 框架提供的一个用于生成代码的命令行工具,可以用于生成 Hertz 项目的脚手架。

### 安装命令行工具 hz

首先,我们需要安装使用本示例所需要的命令行工具 hz:

1. 确保 `GOPATH` 环境变量已经被正确地定义(例如 `export GOPATH=~/go`)并且将 `$GOPATH/bin` 添加到 `PATH` 环境变量之中(例如 `export PATH=$GOPATH/bin:$PATH`);请勿将 `GOPATH` 设置为当前用户没有读写权限的目录。
2. 安装 hz:`go install github.com/cloudwego/hertz/cmd/hz@latest`

更多 hz 使用方法可参考: [hz](https://www.cloudwego.io/zh/docs/hertz/tutorials/toolkit/)

### 确定代码放置位置

1. 若将代码放置于 `$GOPATH/src` 下,需在 `$GOPATH/src` 下创建额外目录,进入该目录后再获取代码:

```bash
mkdir -p $(go env GOPATH)/src/github.com/cloudwego
cd $(go env GOPATH)/src/github.com/cloudwego
```

2. 若将代码放置于 `GOPATH` 之外,可直接获取。

### 生成/编写示例代码

1. 在当前目录下创建 hertz_demo 文件夹,进入该目录中。
2. 生成代码 `hz new`,若当前不在 `GOPATH`,需要添加 `-module` 或者 `-mod` flag 指定一个自定义的模块名称。详细参考[这里](https://www.cloudwego.io/zh/docs/hertz/tutorials/toolkit/usage/usage/)
3. 整理 & 拉取依赖。

```bash
go mod init # 当前目录不在 GOPATH 下不需要 `go mod init` 这一步
go mod tidy
```

### 运行示例代码

完成以上操作后,我们可以直接编译并启动 Server
完成以上操作后,我们可以直接编译并启动 Server

```bash
go build -o hertz_demo && ./hertz_demo
```

如果成功启动,你将看到以下信息
如果成功启动,你将看到以下信息

```bash
2022/05/17 21:47:09.626332 engine.go:567: [Debug] HERTZ: Method=GET absolutePath=/ping --> handlerName=main.main.func1 (num=2 handlers)
2022/05/17 21:47:09.629874 transport.go:84: [Info] HERTZ: HTTP server listening on address=[::]:8888
```

接下来,我们可以对接口进行测试
接下来,我们可以对接口进行测试

```bash
curl http://127.0.0.1:8888/ping
```

如果不出意外,我们可以看到类似如下输出
如果不出意外,我们可以看到类似如下输出

```bash
{"message":"pong"}
```

到现在,我们已经成功启动了 Hertz Server,并完成了一次调用。更多 API 示例请参考 [API 示例](https://pkg.go.dev/github.com/cloudwego/hertz)

## 目录结构

关于项目目录结构组织,这里有一个[目录结构](https://github.com/golang-standards/project-layout)可供参考,具体可以根据业务的实际情况进行组织
到现在,我们已经成功启动了 Hertz Server,并完成了一次调用。

## 更多示例

参考:[hertz-examples](https://github.com/cloudwego/hertz-examples)
参考:[代码示例](/zh/docs/hertz/tutorials/example/)

1 comment on commit b19225d

@vercel
Copy link

@vercel vercel bot commented on b19225d Aug 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.