原文連接:http://targetliu.com/vscode-can-not-go-to-def/android
以前研究GOLANG時一直用LiteIDE,不得不說,LiteIDE的確不錯,可是總感受缺少美感,是一款很中規中矩的編輯器。網上看到你們對VSCODE評價不錯,嘗試後發現的確不錯,佈局簡潔、插件化、支持中文,經過VSCODE GO擴展可以很舒服的寫GO的代碼。git
不過在實際使用過程當中發現 net
包沒法正常跳轉到定義,以下段代碼 ResolveTCPAddr
就沒法正常跳轉github
package main import ( "net" ) func main() { _, err := net.ResolveTCPAddr("tcp", ":4040") }
因爲VSCODE GO中跳轉到定義使用的是godef,遂經過godef的debug模式查看
問題緣由:golang
godef -debug -f main.go net.ResolveTCPAddr
運行結果以下:tcp
2016/08/02 01:17:30 exprType tuple:false pkg: *ast.SelectorExpr net.ListenTCP [ 2016/08/02 01:17:30 exprType tuple:false pkg: *ast.Ident net [ 2016/08/02 01:17:30 exprType tuple:false pkg: *ast.ImportSpec "net" [ 2016/08/02 01:17:30 ] -> 0x0, Type{package "" *ast.ImportSpec "net"} 2016/08/02 01:17:30 ] -> 0xc820157860, Type{package "" *ast.ImportSpec "net"} 2016/08/02 01:17:30 member Type{package "" *ast.ImportSpec "net"} 'ListenTCP' { 2016/08/02 01:17:30 /usr/local/go/src/net/cgo_android.go:10:8: cannot find identifier for package "C": cannot find package "C" in any of: /usr/local/go/src/vendor/C (vendor tree) /usr/local/go/src/C (from $GOROOT) /Users/targetliu/dev/govendor/src/C (from $GOPATH) /Users/targetliu/dev/golang/src/C 2016/08/02 01:17:30 } -> <nil> 2016/08/02 01:17:30 ] -> 0x0, Type{bad "" <nil> } 2016/08/02 01:17:30 exprType tuple:false pkg: *ast.SelectorExpr net.ListenTCP [ 2016/08/02 01:17:30 exprType tuple:false pkg: *ast.Ident net [ 2016/08/02 01:17:30 exprType tuple:false pkg: *ast.ImportSpec "net" [ 2016/08/02 01:17:30 ] -> 0x0, Type{package "" *ast.ImportSpec "net"} 2016/08/02 01:17:30 ] -> 0xc820157860, Type{package "" *ast.ImportSpec "net"} 2016/08/02 01:17:30 member Type{package "" *ast.ImportSpec "net"} 'ListenTCP' { 2016/08/02 01:17:30 /usr/local/go/src/net/cgo_android.go:10:8: cannot find identifier for package "C": cannot find package "C" in any of: /usr/local/go/src/vendor/C (vendor tree) /usr/local/go/src/C (from $GOROOT) /Users/targetliu/dev/govendor/src/C (from $GOPATH) /Users/targetliu/dev/golang/src/C 2016/08/02 01:17:30 } -> <nil> 2016/08/02 01:17:30 ] -> 0x0, Type{bad "" <nil> } godef: no declaration found for net.ListenTCP
注意到這一句:編輯器
cannot find identifier for package "C": cannot find package "C" in any of:
原來是 net
包裏 import C
,然而C並非一個具體真實存在的包,因此godef沒法進行分析,致使找不到定義。ide
godef的GitHub上做者也發現了一樣的問題:Issue:net.LookupIP fails #41佈局
在godef的GitHub上看到有人提交了針對這個問題的解決方案:master - Special treatment for "C" package. #44插件
根據這個提交,能夠嘗試使用以下方法解決:debug
找到並打開godef的 go/parser/parser.go
這個文件
在 1970行
左右添加(代碼中+號部分,能夠經過搜索定位):
if declIdent == nil { filename := p.fset.Position(path.Pos()).Filename name, err := p.pathToName(litToString(path), filepath.Dir(filename)) + if litToString(path) == "C" { + name = "C" + } if name == "" { p.error(path.Pos(), fmt.Sprintf("cannot find identifier for package %q: %v", litToString(path), err)) } else {
從新編譯godef
若是遇到一樣問題的同窗不妨試一試以上方式,至少對於我來講,問題獲得瞭解決。也但願做者能儘快修復這個問題。