运行简单的 Go 程序时出现这么长的错误。这是我的
main.go
,它在一个文件中包含所有功能(否则 vscode 会给我错误,即使所有文件都使用 package main
)。
main.go
它所做的只是从 api 调用一些免费数据并将其打印在 Fyne 窗口中。
package main
import (
"C"
"fmt"
"io"
"net/http"
"time"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/widget"
)
func Call2(someurl string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, someurl, nil)
if err != nil {
fmt.Printf("Error creating request: %s", err)
return nil, err
}
client := http.Client{
Timeout: 30 * time.Second,
}
res, err := client.Do(req)
if err != nil {
fmt.Printf("Error making HTTP request: %s", err)
return nil, err
}
return res, nil
}
func tidyUp() {
fmt.Println("Exited")
}
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("Hello")
k, err := Call2("https://acre.iex.cloud/v1/data/CORE/REF_DATA?token=pk_79891f5cbcce4099ae063588956f937a")
if err != nil {
fmt.Printf("Error making HTTP request: %s", err)
return
}
defer k.Body.Close()
b, err := io.ReadAll(k.Body)
if err != nil {
fmt.Printf("Error reading response body: %s", err)
return
}
// fmt.Println(string(b))
myWindow.SetContent(widget.NewLabel(string(b)))
myWindow.Show()
myApp.Run()
tidyUp()
}
这是(很长)错误的开始:
Exception 0xc0000005 0x0 0x7ffbd991a0f0 0x7ff89943445e
PC=0x7ff89943445e
signal arrived during external code execution
runtime.cgocall(0x7ff6ead942c0, 0xc000065da0)
C:/Program Files/Go/src/runtime/cgocall.go:157 +0x3e fp=0xc000065d78 sp=0xc000065d40 pc=0x7ff6ea9655be
github.com/go-gl/glfw/v3.3/glfw._Cfunc_glfwMakeContextCurrent(0x2157624b2d0)
错误到此结束:
C:/Program Files/Go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc01494dfe8 sp=0xc01494dfe0 pc=0x7ff6ea9c8ac1
created by os/signal.Notify.func1.1 in goroutine 55
C:/Program Files/Go/src/os/signal/signal.go:151 +0x1f
rax 0xfff0fff
rbx 0xd075dff3a0
rcx 0x7ffbd991a0cc
rdi 0x7ff899c26100
rsi 0x2157c392b20
rbp 0xd075dfefb0
rsp 0xd075dfeea8
r8 0xd075dfefc0
r9 0xd075dff3a0
r10 0xd075dff190
r11 0x0
r12 0x100
r13 0x0
r14 0x1
r15 0x0
rip 0x7ff89943445e
rflags 0x10206
cs 0x33
fs 0x53
gs 0x2b
exit status 2
看起来这只是 VScode 的问题。重新启动就可以了。