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
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 }
View Code

 


 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM