gpt-3.5-turbo-0125· OpenAI
WildChat-4.8M (AI2)
Go 语言读取当前目录下的public作为静态文件服务器的入口,并且实现 代理接口功能,当用户访问/prod-api,转发到 127.0.0.1:8095 端口…
- User
User: Go 语言读取当前目录下的public作为静态文件服务器的入口,并且实现 代理接口功能,当用户访问/prod-api,转发到 127.0.0.1:8095 端口 Assistant: 以下是一个用 Go 语言实现的简单例子,实现了读取当前目录下的public作为静态文件服务器的入口,并且实现了代理接口功能:
package mainimport ( “net/http” “net/http/httputil” “net/url” “os”)func main() { // 设置静态文件服务器 fs := http.FileServer(http.Dir(“public”)) http.Handle(“/”, fs) // 设置代理接口 proxy := httputil.NewSingleHostReverseProxy(&url.URL{ Scheme: “http”, Host: “127.0.0.1:8095”, }) http.Handle(“/prod-api”, http.StripPrefix(“/prod-api”, proxy)) // 启动服务器 port := “8080” if len(os.Args) > 1 { port = os.Args[1] } println(“Server started at http://localhost:” + port) err := http.ListenAndServe(“:”+port, nil) if err != nil { println(“Server error:”, err) }}在当前目录下创建一个名为public的文件夹,里面放入你想要提供的静态文件。然后运行以上代码,在浏览器中访问http://localhost:8080/可以访问静态文件服务器,访问http://localhost:8080/prod-api则会转发到127.0.0.1:8095端口。 User: Write s simple go hello world Assistant:
- Assistant
Sure! Here is a simple Go program that prints "Hello, World!":
package main import "fmt" func main() { fmt.Println("Hello, World!") }You can run this program by saving it in a file with a
.goextension (e.g.,hello.go) and then runninggo run hello.goin your command line. It will outputHello, World!to the console.