php程序员学go语言(三)-Gin 极速入门
2026/6/8 14:57:04 网站建设 项目流程

go没入门基础语法的看文章: php程序员学go语言(一)

一、创建项目 + 安装 Gin

# 创建项目文件夹 mkdir gin-demo && cd gin-demo #初始化项目(类似 composer init) go mod init gin-demo # 1. 开启模块 go env -w GO111MODULE=on # 2. 设置国内代理(七牛云,最稳) go env -w GOPROXY=https://goproxy.cn,direct # 3. 关闭国外校验库(国内连不上) go env -w GOSUMDB=off #安装 Gin(类似 composer require) go get github.com/gin-gonic/gin

二、第一个gin框架的程序(对标 TP 入口文件)

package main // 导入Gin框架 import "github.com/gin-gonic/gin" func main() { // 1. 创建Gin实例(相当于TP初始化应用) router := gin.Default() // 2. 定义路由(对标TP路由:Route::get('/', 'Index@index');) router.GET("/", func(c *gin.Context) { c.String(200, "Hello Gin! 我从PHP转来啦~") }) // 3. 启动服务(默认端口8080,如果这个端口被占用可以换其他端口) router.Run(":8080") }

执行:go run main.go

打开浏览器访问http://localhost:8080 或 http://127.0.0.1:8080,看到输出就成功了!🎉

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询