安装与配置 #
一、环境准备 #
1.1 安装Go #
Echo需要Go 1.18或更高版本。
macOS
bash
brew install go
Linux (Ubuntu/Debian)
bash
sudo apt update
sudo apt install golang-go
Windows
从 Go官网 下载安装包。
1.2 验证安装 #
bash
go version
go version go1.21.0 darwin/arm64
1.3 配置环境变量 #
bash
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
export GO111MODULE=on
添加到 ~/.bashrc 或 ~/.zshrc 使其永久生效。
1.4 配置代理(国内用户) #
bash
go env -w GOPROXY=https://goproxy.cn,direct
go env -w GOSUMDB=sum.golang.google.cn
二、安装Echo #
2.1 创建项目目录 #
bash
mkdir myapp
cd myapp
2.2 初始化Go模块 #
bash
go mod init myapp
输出:
text
go: creating new go.mod: module myapp
2.3 安装Echo #
bash
go get github.com/labstack/echo/v4
2.4 验证安装 #
查看 go.mod 文件:
text
module myapp
go 1.21
require github.com/labstack/echo/v4 v4.11.1
三、项目初始化 #
3.1 基本项目结构 #
text
myapp/
├── main.go
├── go.mod
└── go.sum
3.2 创建main.go #
go
package main
import (
"net/http"
"github.com/labstack/echo/v4"
)
func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, Echo!")
})
e.Logger.Fatal(e.Start(":8080"))
}
3.3 运行项目 #
bash
go run main.go
输出:
text
____ __
/ __/___/ / ___
/ _// __/ _ \/ _ \
/___/\__/_//_/\___/ v4.11.1
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
O\
⇨ http server started on [::]:8080
3.4 测试访问 #
bash
curl http://localhost:8080
Hello, Echo!
四、开发工具配置 #
4.1 VS Code配置 #
安装Go扩展:
text
扩展ID: golang.go
创建 .vscode/settings.json:
json
{
"go.useLanguageServer": true,
"go.lintTool": "golangci-lint",
"go.lintOnSave": "package",
"go.formatTool": "goimports",
"editor.formatOnSave": true
}
4.2 GoLand配置 #
- 打开 Settings/Preferences
- Go → GOROOT:设置Go安装路径
- Go → Go Modules:启用Go modules集成
- Editor → Code Style → Go:配置代码风格
4.3 安装开发工具 #
bash
go install golang.org/x/tools/gopls@latest
go install github.com/go-delve/delve/cmd/dlv@latest
go install mvdan.cc/gofumpt@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
五、热重载配置 #
5.1 使用Air #
安装Air:
bash
go install github.com/cosmtrek/air@latest
初始化配置:
bash
air init
创建 .air.toml:
toml
root = "."
tmp_dir = "tmp"
[build]
cmd = "go build -o ./tmp/main ."
bin = "./tmp/main"
full_bin = "./tmp/main"
include_ext = ["go", "tpl", "tmpl", "html"]
exclude_dir = ["assets", "tmp", "vendor"]
delay = 1000
运行:
bash
air
5.2 使用Fresh #
安装Fresh:
bash
go install github.com/gravityblast/fresh@latest
运行:
bash
fresh
六、项目配置 #
6.1 使用配置文件 #
创建 config/config.yaml:
yaml
app:
name: myapp
port: 8080
debug: true
database:
driver: mysql
host: localhost
port: 3306
name: myapp
user: root
password: secret
redis:
host: localhost
port: 6379
db: 0
6.2 读取配置 #
安装Viper:
bash
go get github.com/spf13/viper
使用配置:
go
package main
import (
"net/http"
"github.com/labstack/echo/v4"
"github.com/spf13/viper"
)
func initConfig() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("./config")
if err := viper.ReadInConfig(); err != nil {
panic(err)
}
}
func main() {
initConfig()
e := echo.New()
port := viper.GetString("app.port")
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, Echo!")
})
e.Logger.Fatal(e.Start(":" + port))
}
6.3 环境变量配置 #
go
package main
import (
"net/http"
"os"
"github.com/labstack/echo/v4"
)
func main() {
e := echo.New()
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, Echo!")
})
e.Logger.Fatal(e.Start(":" + port))
}
七、依赖管理 #
7.1 常用命令 #
bash
go mod init myapp
go mod tidy
go mod download
go mod verify
go mod graph
7.2 添加依赖 #
bash
go get github.com/labstack/echo/v4
go get github.com/labstack/echo/v4/middleware
go get gorm.io/gorm
go get gorm.io/driver/mysql
7.3 更新依赖 #
bash
go get -u github.com/labstack/echo/v4
go get -u=patch github.com/labstack/echo/v4
go mod tidy
7.4 指定版本 #
bash
go get github.com/labstack/echo/v4@v4.11.1
go get github.com/labstack/echo/v4@latest
八、常见问题 #
8.1 代理问题 #
问题:dial tcp: lookup proxy.golang.org: no such host
解决:
bash
go env -w GOPROXY=https://goproxy.cn,direct
8.2 版本冲突 #
问题:version "v4.x.x" invalid
解决:
bash
go clean -modcache
go mod download
8.3 模块未找到 #
问题:package xxx is not in GOROOT
解决:
bash
go mod tidy
go mod download
8.4 端口占用 #
问题:bind: address already in use
解决:
bash
lsof -i :8080
kill -9 <PID>
九、IDE调试配置 #
9.1 VS Code launch.json #
创建 .vscode/launch.json:
json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Echo Server",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"env": {
"PORT": "8080"
},
"args": []
}
]
}
9.2 GoLand运行配置 #
- Run → Edit Configurations
- 添加 Go Build
- 配置:
- Package path: 项目路径
- Working directory: 项目根目录
- Environment: PORT=8080
十、总结 #
安装配置要点:
| 步骤 | 说明 |
|---|---|
| 环境准备 | 安装Go 1.18+,配置环境变量 |
| 项目初始化 | go mod init,创建项目结构 |
| 安装Echo | go get github.com/labstack/echo/v4 |
| 开发工具 | VS Code/GoLand + Go扩展 |
| 热重载 | Air/Fresh实现自动重启 |
| 配置管理 | Viper读取配置文件 |
准备好创建第一个Echo应用了吗?让我们进入下一章!
最后更新:2026-03-28