Skip to content
鼓励作者:欢迎打赏犒劳

web初体验

第一个web服务

go
package main

import (
	"fmt"
	"log"
	// 第三方库导入
	"github.com/gin-gonic/gin"
)

func main() {
	startWebServer()
}

func startWebServer() {
	r := gin.Default()

	r.GET("/", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "Hello from Gin!",
			"quote":   "HelloHelloHelloHelloHello",
		})
	})

	r.GET("/hello/:name", func(c *gin.Context) {
		name := c.Param("name")
		c.JSON(200, gin.H{
			"message": fmt.Sprintf("Hello, %s! Welcome to Go!", name),
		})
	})

	log.Println("服务器运行在 http://localhost:8080")
	r.Run(":8080")
}

如有转载或 CV 的请标注本站原文地址