创建gomod项目
go mod init web01
新建main.go
package main
import (
"fmt"
"net/http"
)
func handler(writer http.ResponseWriter, request *http.Request) {
fmt.Fprintf(writer, "Hello World,%s!", request.URL.Path[1:])
}
func main() {
fmt.Println("http:127.0.0.1:8080")
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
创建launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
}
]
}
启动项目访问http://127.0.0.1:8080/