Golang程序开发遇到问题求助
Golang程序开发遇到问题求助 大家好,
我在执行程序时遇到了一个问题
笔记本电脑上安装的Golang版本
Rajeshs-MacBook-Pro:03_ParseForm_Form rajeshjain$ go version go version go1.8.3 darwin/amd64
运行程序时出现以下错误
Rajeshs-MacBook-Pro:03_ParseForm_Form rajeshjain$ go run main.go
# runtime
/usr/local/go/src/runtime/cgo.go:9:3: //go:cgo_export_static main only allowed in cgo-generated code
很抱歉,我是Golang的新手。
我正在运行的程序如下:
程序
package main
import (
"html/template"
"log"
"net/http"
"fmt"
)
type hotdog int
func (m hotdog) ServeHTTP(w http.ResponseWriter, req *http.Request) {
fmt.Println("I am in ServeHTTP")
/*
ParseForm is the method which needs to be call before we can use
req.Form - This for getting the data in the body
req.PostForm - this is for getting the data from body and header
*/
err := req.ParseForm()
if err != nil {
log.Fatalln(err)
}
fmt.Println(req.Form)
tpl.ExecuteTemplate(w, "index.html", req.Form)
}
var tpl *template.Template
func init() {
tpl = template.Must(template.ParseFiles("index.html"))
}
func main() {
var d hotdog
http.ListenAndServe(":7777", d)
}
更多关于Golang程序开发遇到问题求助的实战教程也可以访问 https://www.itying.com/category-94-b0.html
Raj_Jain:
main only allowed in cgo-generated code
看起来你的 Go 版本太旧了 cgo_export_static main only allowed in cgo-generated code · Issue #125 · denoland/deno · GitHub。请更新到最新的 1.11 版本。
更多关于Golang程序开发遇到问题求助的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
移除 brew 版本。
此外,PATH 是从左到右检查的,而你把"新"路径放在了最右边,所以系统可能会先找到旧版本。你需要把新路径放在左边,或者确保彻底移除旧版本。
另外,根据经验法则,用户安装的程序应该始终比系统安装的程序优先被找到,因此在大多数情况下,你应该设置 PATH=/新路径/bin:${PATH}。
更新到最新的 1.11 版本
嗨 Johan Dahl,
感谢您的回复。 在我的 Mac 上开始学习时,我使用 brew 安装了 Go,它将其设置为:
export GOROOT=/usr/local/opt/go/libexec
这原本工作正常……现在我从官方网站安装了新版本的 Go,可以看到它安装在 “/usr/local/go”。 现在我尝试将 GOROOT 设置为 /usr/local/go。 以下是我在 .bash_profile 中设置的参数:
export GOPATH=[path]/projects/GoProjects
export GOROOT=/usr/local/go
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
但当我运行 “go version” 时,仍然显示 “go version go1.8.3 darwin/amd64”,这是我通过 brew 安装 Go 时的版本。
谢谢,
这个错误是由于Go版本兼容性问题导致的。Go 1.8.3是一个较旧的版本,可能与某些系统环境不兼容。以下是解决方案:
解决方案:升级Go版本
建议升级到更新的Go版本,如Go 1.19+。可以通过以下步骤升级:
# 卸载旧版本
sudo rm -rf /usr/local/go
# 下载并安装新版本
# 从 https://golang.org/dl/ 下载适合你系统的版本
tar -C /usr/local -xzf go1.19.linux-amd64.tar.gz
# 添加到PATH
export PATH=$PATH:/usr/local/go/bin
代码优化建议
你的程序本身没有语法错误,但可以做一些改进:
package main
import (
"html/template"
"log"
"net/http"
"fmt"
)
type hotdog int
func (m hotdog) ServeHTTP(w http.ResponseWriter, req *http.Request) {
fmt.Println("I am in ServeHTTP")
err := req.ParseForm()
if err != nil {
log.Fatalln(err)
}
fmt.Printf("Form data: %v\n", req.Form)
// 添加错误处理
err = tpl.ExecuteTemplate(w, "index.html", req.Form)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
var tpl *template.Template
func init() {
var err error
tpl, err = template.ParseFiles("index.html")
if err != nil {
log.Fatal("Error parsing template:", err)
}
}
func main() {
var d hotdog
fmt.Println("Server starting on :7777")
err := http.ListenAndServe(":7777", d)
if err != nil {
log.Fatal("Server error:", err)
}
}
验证升级后的环境
升级后验证Go版本:
go version
应该显示类似:
go version go1.19 darwin/amd64
升级Go版本后,这个cgo相关的编译错误应该会消失。你的HTTP服务器代码逻辑是正确的,升级环境即可解决这个问题。

