Go语言 踩坑录(报错锦集)
# 设置cmd代理
set http_proxy=http://127.0.0.1:10809
set https_proxy=http://127.0.0.1:10809
set https_proxy=socks5://127.0.0.1:10808
set http_proxy=socks5://127.0.0.1:10808
# git
git config --global http.proxy socks5://127.0.0.1:1080 # Recover git global config: git config --global --unset http.proxy
# Golang:Delve版本太低无法Debug
Version of Delve is too old for this version of Go (maximum supported version 1.13, suppress this error with --check-go-version=false)
cause: dlv版本低
solution:直接去官网download https://github.com/go-delve/delve
替换你go目录下的 %GOPATH%\src\github.com\go-delve 这个目录即可
# Go程序报错1
X:\PycharmProjects\Project\TmpTest\GO_tmp\.vscode\Temp_Go\frist_go.go:7:6: main redeclared in this block previous declaration at X:\PycharmProjects\Project\TmpTest\GO_tmp\.vscode\Temp_Go\demo_1.go:13:6
exit status 2
Process exiting with code: 1
cause:同一个目录下面不能有个多 package main
solution:新建一个目录放置这个第二次调用main的代码
# Go程序报错2
fatal error: all goroutines are asleep - deadlock!
cause: 产生死锁,超出管道设定容量
solution:扩大设定管道的容量或者减少超过的管道
# Go程序报错3
package main
import "fmt"
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return
}
func main() {
fmt.Println("result is -->: ", split(17)) // 这个地方限制了仅有一个返回值
}
# command-line-arguments
.\compile19.go:12:24: multiple-value split() in single-value context
cause: 返回值有多个值
solution: 两种改法:
1) 直接把前面的字符型注释清空,仅保留后面有多个值反馈的函数
2) 把多值的函数内容单独赋值给变量,在输出位置单独应用每个变量
# Go程序报错4
Tips: 一个关于编译简写定义变量出错提示
syntax error: non-declaration statement outside function body
cause: 在全局变量使用了简写的变量定义和赋值
solution: 使用完整的定义方式
# 代码演示

1 package main
2
3 import (
4 "fmt"
5 )
6
7 var a int = 100 // 正确方式,不如编译会出错
8
9 // a :=100 这种简写仅能存在函数内,全局变量只能定义不能赋值
10
11 /** a:= 100 ,实际上是两条语句组成,如下:
12 var a int
13 a = 100 <--这个位置有赋值操作,所以定义在全局变量下会如下报错:
14 "syntax error: non-declaration statement outside function body"
15 **/
16
17 func main() {
18 b := 100 // 函数体内可以这么玩
19
20 fmt.Println(a)
21 fmt.Println(b)
22
23 }
标签:
Go
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库