# 快速开始

# Example

项目地址 (opens new window)

# 创建项目

mkdir helloworld 
cd helloworld 

go mod init helloworld
1
2
3
4

# HelloWorld

package main

import (
   "github.com/gin-gonic/gin"
   "github.com/gotomicro/ego"
   "github.com/gotomicro/ego/core/elog"
   "github.com/gotomicro/ego/server/egin"
)
//export EGO_DEBUG=true && go run main.go --config=config.toml
func main() {
   if err := ego.New().Serve(func() *egin.Component {
      server := egin.Load("server.http").Build()
      server.GET("/hello", func(ctx *gin.Context) {
         ctx.JSON(200, "Hello Ego")
         return
      })
      return server
   }()).Run(); err != nil {
      elog.Panic("startup", elog.Any("err", err))
   }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# 安装依赖

go get ./...

go mod download
1
2
3

# 添加配置

  • 在项目中增加config.toml配置文件,内容如下
[server.http]
    port = 9001
    host = "0.0.0.0"
1
2
3

# 使用命令行运行

export EGO_DEBUG=true # 默认日志输出到logs目录,开启后日志输出到终端
go run main.go --config=config.toml
1
2

如下所示

这个时候我们可以发送一个指令,得到如下结果

➜  helloworld git:(master)curl http://127.0.0.1:9001/hello
"Hello Ego"%  
1
2
上次更新: 2024-01-25 09:16:20