Go 语言 IDE 之 VSCode 配置使用

Gogland 是 JetBrains 公司推出的 Go 语言集成开发环境。Gogland 同样基于 IntelliJ 平台开发,支持 JetBrains 的插件体系。官方:https://www.jetbrains.com/go/。关于 Goland 相关配置参考该链接即可。Goland 用的好好的,为

VSCode 是目前比较流行的 IDE 工具,在功能方面也相对齐全,使用方面也比较友好。

一、Golang 环境配置

关于 Goland 的系统环境配置,这里不做讲解。

二、安装配置 VSCode

下载链接:https://code.visualstudio.com/

2.1 安装 golang 插件

进入Visual Studio Code,使用快捷键F1,打开命令面板

在上图光标处·输入 exten ,然后选择“Extensions:Install Extension”,如下图

查询并安装 Go 插件

提示:在插件列表中,选择 Go,进行安装,安装之后,系统会提示重启Visual Studio Code。由于我这里已经安装过了,所以不会出现 "Install" 选项。

2.2 开启自动保存功能

打开Visual Studio Code,找到菜单File -> Preferences -> Settings,如下图:

选中 "User Settings" 配置如下内容

 

{
    "files.autoSave": "onFocusChange",
    "editor.fontSize": 18,
    "go.goroot": "D:\\Go",
    "go.gopath": "D:\\Golang\\MyGO"
}

 

其他保持默认配置即可;

2.3 执行相关命令(自行安装好git)

更多详细信息参考:https://marketplace.visualstudio.com/items?itemName=ms-vscode.Go

1)安装gocode

打开命令提示符(以管理员身份打开),输入:

go get -u -v github.com/nsf/gocode

2)安装godef

go get -u -v github.com/rogpeppe/godef

3)安装golint

 

# 创建 $GOPATH/src/golang.org/x
cd $GOPATH/src/golang.org/x
git clone https://github.com/golang/tools.git
git clone https://github.com/golang/lint.git

# 完成以上步骤后,执行
go get golang.org/x/lint/golint

 

如果你遇到了类似如下错误,就可以通过 git clone 的方式把包下载到本地,再进行安装即可解决问题;

Fetching https://golang.org/x/tools/go/buildutil?go-get=1

https fetch failed: Get https://golang.org/x/tools/go/buildutil?go-get=1: 

dial tcp 216.239.37.1:443: 

connectex: A connection attempt failed because the connected party did not properly 

respond after a period of time, 

or established connection failed because connected host has failed to respond.

package golang.org/x/tools/go/buildutil: unrecognized import path 

"golang.org/x/tools/go/buildutil" (https fetch: Get 

https://golang.org/x/tools/go/buildutil?go-get=1: dial tcp 216.239.37.1:443: 

connectex: A connection attempt failed because the connected party did not properly 

respond after a period of time, 

or established connection failed because connected host has failed to respond.)

4)安装go-find-references

go get -u -v github.com/lukehoban/go-find-references

5)安装go-outline

go get -u -v github.com/lukehoban/go-outline

6)安装goreturns

go get -u -v sourcegraph.com/sqs/goreturns

7)安装gorename

go get -u -v golang.org/x/tools/cmd/gorename

提示:如果还提示报错,因为前面已经git clone 下来了,可以执行执行"go install golang.org/x/tools/cmd/gorename";

8)安装gopkgs

go get -u -v github.com/tpng/gopkgs

9)安装go-symbols

go get -u -v github.com/newhook/go-symbols

2.4 写一个程序进行测试

可以看到能够进行相关提示。这里我们执行完整程序测试一下,程序是否正常。

OK,完全没有问题了。

 

发布了141 篇原创文章 · 获赞 152 · 访问量 60万+
展开阅读全文

如何导入golang自己的软件包?

05-19

<div class="post-text" itemprop="text"> <p>I have problems with importing a package. I want to import the <code>"golang.org/x/tools/cmd/oracle"</code> and the <code>"golang.org/x/tools/oracle"</code> package.</p> <p>Which is the correct way to import these packages?</p> <hr> <p>This the way I try to import the packages:</p> <pre><code>package main import ( "fmt" "strings" "os" "os/exec" "golang.org/x/tools/oracle" "golang.org/x/tools/cmd/oracle" ) </code></pre> <p>The both last imports leads to the following problem:</p> <pre><code>==================== Starting Go build ==================== Cleared problem markers for ProjektGO. ************ Building Go project: ProjektGO ************ with GOPATH: /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO >> Running: /usr/lib/go/bin/go install -v -gcflags "-N -l" ./... file/main.go:13:2: cannot find package "golang.org/x/tools/cmd/oracle" in any of: /usr/lib/go/src/golang.org/x/tools/cmd/oracle (from $GOROOT) /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/cmd/oracle (from $GOPATH) file/main.go:12:2: cannot find package "golang.org/x/tools/oracle" in any of: /usr/lib/go/src/golang.org/x/tools/oracle (from $GOROOT) /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/oracle (from $GOPATH) ^^^ Terminated, exit code: 1 ^^^ ************ Build terminated. ************ </code></pre> <hr> <p>Then I tried things like that:</p> <pre><code>package main import ( "fmt" "strings" "os" "os/exec" "../../../../../golang.org/x/tools/cmd/oracle" "../../../../../golang.org/x/tools/oracle" ) </code></pre> <p>And the result was this:</p> <pre><code>==================== Starting Go build ==================== ************ Building Go project: ProjektGO ************ with GOPATH: /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO >> Running: /usr/lib/go/bin/go install -v -gcflags "-N -l" ./... can't load package: /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/file/main.go:11:2: import "../../../../../golang.org/x/tools/cmd/oracle" is a program, not an importable package ../../../../golang.org/x/tools/oracle/describe.go:20:2: cannot find package "golang.org/x/tools/go/ast/astutil" in any of: /usr/lib/go/src/golang.org/x/tools/go/ast/astutil (from $GOROOT) /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/go/ast/astutil (from $GOPATH) ../../../../golang.org/x/tools/cmd/oracle/main.go:26:2: cannot find package "golang.org/x/tools/go/buildutil" in any of: /usr/lib/go/src/golang.org/x/tools/go/buildutil (from $GOROOT) /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/go/buildutil (from $GOPATH) ../../../../golang.org/x/tools/oracle/callers.go:11:2: cannot find package "golang.org/x/tools/go/callgraph" in any of: /usr/lib/go/src/golang.org/x/tools/go/callgraph (from $GOROOT) /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/go/callgraph (from $GOPATH) ../../../../golang.org/x/tools/cmd/oracle/main.go:27:2: cannot find package "golang.org/x/tools/go/loader" in any of: /usr/lib/go/src/golang.org/x/tools/go/loader (from $GOROOT) /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/go/loader (from $GOPATH) ../../../../golang.org/x/tools/oracle/callees.go:17:2: cannot find package "golang.org/x/tools/go/pointer" in any of: /usr/lib/go/src/golang.org/x/tools/go/pointer (from $GOROOT) /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/go/pointer (from $GOPATH) ../../../../golang.org/x/tools/oracle/callees.go:18:2: cannot find package "golang.org/x/tools/go/ssa" in any of: /usr/lib/go/src/golang.org/x/tools/go/ssa (from $GOROOT) /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/go/ssa (from $GOPATH) ../../../../golang.org/x/tools/oracle/callees.go:19:2: cannot find package "golang.org/x/tools/go/ssa/ssautil" in any of: /usr/lib/go/src/golang.org/x/tools/go/ssa/ssautil (from $GOROOT) /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/go/ssa/ssautil (from $GOPATH) ../../../../golang.org/x/tools/oracle/describe.go:22:2: cannot find package "golang.org/x/tools/go/types/typeutil" in any of: /usr/lib/go/src/golang.org/x/tools/go/types/typeutil (from $GOROOT) /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/go/types/typeutil (from $GOPATH) ../../../../golang.org/x/tools/cmd/oracle/main.go:28:2: cannot find package "golang.org/x/tools/oracle" in any of: /usr/lib/go/src/golang.org/x/tools/oracle (from $GOROOT) /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/oracle (from $GOPATH) ../../../../golang.org/x/tools/oracle/callees.go:20:2: cannot find package "golang.org/x/tools/oracle/serial" in any of: /usr/lib/go/src/golang.org/x/tools/oracle/serial (from $GOROOT) /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/oracle/serial (from $GOPATH) ../../../../golang.org/x/tools/oracle/implements.go:21:2: cannot find package "golang.org/x/tools/refactor/importgraph" in any of: /usr/lib/go/src/golang.org/x/tools/refactor/importgraph (from $GOROOT) /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/refactor/importgraph (from $GOPATH) ^^^ Terminated, exit code: 1 ^^^ ************ Build terminated. ************ </code></pre> <hr> <p>My GOROOT is <code>/usr/lib/go</code> and my GOPATH is <code>/home/haapoo/go</code> . I know the question about gopath and goroot comes often but maybe anyone has an idea.</p> <p>PS: I use goclipse</p> <p>Greetings haapoo</p> </div> 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览