帮我用go写一段代码,实现以下两个需求:1.传入参数作为端口,并在本地IP上启动这个端口的服务;2.访问这个端口可以返回文字“hello world”
时间: 2023-03-21 15:04:00 浏览: 52
好的,以下是使用Go语言编写的代码,满足您提出的需求:
```
package main
import (
"fmt"
"net/http"
)
func main() {
port := ":8080" // 端口号
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "helloworld")
})
err := http.ListenAndServe(port, nil)
if err != nil {
fmt.Println(err)
}
}
```
该代码会在本地IP的8080端口启动一个服务,并在访问该端口时返回"helloworld"。