在Debian上使用Golang进行Web开发,可以按照以下步骤进行:
sudo apt update
sudo apt install golang
go version
echo 'export GOPATH=$HOME/go' >> ~/.bashrc
echo 'export PATH=$PATH:$GOPATH/bin' >> ~/.bashrc
source ~/.bashrc
server.go
,并添加以下代码:package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, you've requested: %s
", r.URL.Path)
})
http.ListenAndServe(":8080", nil)
}
go build server.go
./server
http://localhost:8080
,你将看到“Hello, you’ve requested: /”的消息。go get -u github.com/gin-gonic/gin
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/hello", func(c *gin.Context) {
c.String(http.StatusOK, "Hello, world!")
})
r.Run(":8000")
}
go run main.go
http://localhost:8000/hello
,你将看到“Hello, world!”的消息。通过以上步骤,你可以在Debian上成功安装Go语言环境,并创建和运行一个简单的Web服务器。使用流行的Web框架如Gin可以进一步简化Web开发过程。希望这些信息对你有所帮助!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:如何在CentOS上使用Golang进行Web开发