为什么80%的码农都做不了架构师?>>>   

基于Visio Studio Code打造go的IDE

[TOC]

前言

​ 最近有点儿时间,把想做的事情列了个清单。开个新坑,学习下go。主要是想从go入手,实践微服务。把VSC编辑器清理了下,打造go的高效IDE。本命年,学习go,应景:)

​ 本文基于macOS High Sierra 10.13.1。

配置本地go环境

软件下载和安装

go SDK

官方网站貌似被墙了,找了国内的go学习社区下载。双击安装。

校验

Last login: Sat Feb 24 15:07:43 on ttys002
ChinaDreams:~ kangcunhua$ go version
go version go1.10 darwin/amd64

配置开发环境

ChinaDreams:~ kangcunhua$ vi .bash_profile
ChinaDreams:~ kangcunhua$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin
ChinaDreams:~ kangcunhua$ . .bash_profile # source .bash_profile 重载使配置生效
ChinaDreams:~ kangcunhua$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Users/kangcunhua/Documents/work-space/go-project/bin
ChinaDreams:~ kangcunhua$ more .bash_profile
# cofing go-project env
export GOPATH=$HOME/Documents/work-space/go-project
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
ChinaDreams:~ kangcunhua$ go env # 可以查看已配置的go环境变量

Hello world

新建源码目录

ChinaDreams:work-space kangcunhua$ cd $GOPATH
ChinaDreams:go-project kangcunhua$ mkdir src # go 源码目录
ChinaDreams:go-project kangcunhua$ cd $GOPATH/src
ChinaDreams:src kangcunhua$ mkdir -p opsdev.ren/hellomath
ChinaDreams:src kangcunhua$ cd opsdev.ren/hellomath/
ChinaDreams:hellomath kangcunhua$ vi sqrt.go
ChinaDreams:hellomath kangcunhua$ go install #安装 到$GOPATH/pkg目录

源码sqrt.go

package mymath
func Sqrt(x float64) float64{z := 0.0for i := 0; i < 1000; i ++{z -= (z * z - x) / (2 * x)}return z
}

新建调用应用 main.go

ChinaDreams:hellomath kangcunhua$ mkdir mathapp
ChinaDreams:hellomath kangcunhua$ cd mathapp
ChinaDreams:mathapp kangcunhua$ vi main.go
ChinaDreams:mathapp kangcunhua$ go build # 该目录下生成一个mathapp的可执行文件
ChinaDreams:mathapp kangcunhua$ ls
main.go mathapp
ChinaDreams:mathapp kangcunhua$ ./mathapp # 执行
Hello,world. Sqrt(2) = 1.414213562373095
ChinaDreams:mathapp kangcunhua$ go install # 安装至bin目录(因为package是main)
ChinaDreams:mathapp kangcunhua$ cd $GOPATH/bin
ChinaDreams:bin kangcunhua$ ls
mathapp
ChinaDreams:bin kangcunhua$ mathapp # 直接执行发布的二进制文件
Hello,world. Sqrt(2) = 1.414213562373095

源码main.go

package main
import("opsdev.ren/hellomath""fmt"
)
func main(){fmt.Printf("Hello,world. Sqrt(2) = %v\n",mymath.Sqrt(2))
}

Go 目录介绍

$GOPATH下有bin、pkg、src三大目录。所有活动都在此进行,以刚刚撰写、编译、安装的的helloworld demo为例,目录结构如下,

ChinaDreams:go-projcet kangcunhua$ tree
.
├── bin
│   └── mathapp
├── pkg
│   └── darwin_amd64
│       └── opsdev.ren
│           └── hellomath.a
└── src└── opsdev.ren└── hellomath├── mathapp│   └── main.go└── sqrt.go8 directories, 4 files
ChinaDreams:go-project kangcunhua$

其中src放置源码

src目录是开发程序的主要目录,所有的源码是放在这个目录下面。 例如:$GOPATH/src/mymath表示mymath这个应用包或者可执行应用,这个是根据package是main还是其他来决定,main的话是可执行应用,其他的话就是应用包.

pkg放置安装的应用、bin放置可执行的应用;

配置Visio Studio Code

软件下载和安装

Visio Studio Code官网下载,双击安装。

安装go插件

在搜索框里输入Go, 找到 Rich Go language support for Visual Studio Code的插件, 点击安装,重启编辑器。

自动安装依赖

拖进一个go文件,选择安装全部依赖

Installing 9 tools at /Users/kangcunhua/Documents/work-space/go-project/bingocodegopkgsgo-outlinego-symbolsgurugorenamegodefgoreturnsgolint

安装依赖包排错和解决

提示安装6个tools失败,

Installing github.com/ramya-rao-a/go-outline FAILED
Installing github.com/acroca/go-symbols FAILED
Installing golang.org/x/tools/cmd/guru FAILED
Installing golang.org/x/tools/cmd/gorename FAILED
Installing sourcegraph.com/sqs/goreturns FAILED
Installing github.com/golang/lint/golint FAILED

分析尝试手工编译安装

进入目录,发现除golang.org下的包,其余源文件均已get,于是手工编译安装: go build 执行报错,提示

ChinaDreams:go-outline kangcunhua$ go build
main.go:14:2: cannot find package "golang.org/x/tools/go/buildutil" in any of:/usr/local/Cellar/go/1.10/libexec/src/golang.org/x/tools/go/buildutil (from $GOROOT)/Users/kangcunhua/Documents/work-space/go-project/src/golang.org/x/tools/go/buildutil (from $GOPATH)

git clone golang tools

好吧,先把整个golang tools clone下来再说:

其实 golang 在 github 上建立了一个镜像库,如 https://github.com/golang/tools 即是 https://golang.org/x/tools 的镜像库

ChinaDreams:x kangcunhua$ git clone git@github.com:golang/tools.git
ChinaDreams:tools kangcunhua$ go get -u -v  github.com/golang/tools/go/buildutil
github.com/golang/tools (download)
package github.com/golang/tools/go/buildutil: code in directory /Users/kangcunhua/Documents/work-space/go-project/src/github.com/golang/tools/go/buildutil expects import "golang.org/x/tools/go/buildutil"

再次手工编译安装

尝试了一个了一个,成功

ChinaDreams:ramya-rao-a kangcunhua$ cd go-outline/
ChinaDreams:go-outline kangcunhua$ ls
LICENSE     README.md   main.go
ChinaDreams:go-outline kangcunhua$ go build
ChinaDreams:go-outline kangcunhua$ go install
ChinaDreams:go-outline kangcunhua$

切回vsc自动安装

在vsc中的go源码界面按了一下cmd + s保存,提示还缺5个依赖包,选择自动安装,虽然慢点儿,但是也成功了。

Installing 5 tools at /Users/kangcunhua/Documents/work-space/go-project/bingo-symbolsgurugorenamegoreturnsgolintInstalling github.com/acroca/go-symbols SUCCEEDED
Installing golang.org/x/tools/cmd/guru SUCCEEDED
Installing golang.org/x/tools/cmd/gorename SUCCEEDED
Installing sourcegraph.com/sqs/goreturns SUCCEEDED
Installing github.com/golang/lint/golint FAILED1 tools failed to install.golint:
Error: Command failed: /usr/local/go/bin/go get -u -v github.com/golang/lint/golint
github.com/golang/lint (download)
Fetching https://golang.org/x/lint?go-get=1
https fetch failed: Get https://golang.org/x/lint?go-get=1: dial tcp 216.239.37.1:443: i/o timeout
package golang.org/x/lint: unrecognized import path "golang.org/x/lint" (https fetch: Get https://golang.org/x/lint?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
github.com/golang/lint (download)
Fetching https://golang.org/x/lint?go-get=1
https fetch failed: Get https://golang.org/x/lint?go-get=1: dial tcp 216.239.37.1:443: i/o timeout
package golang.org/x/lint: unrecognized import path "golang.org/x/lint" (https fetch: Get https://golang.org/x/lint?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)

最后一个手工安装

ChinaDreams:github.com kangcunhua$ cd golang/lint
ChinaDreams:lint kangcunhua$ go build
ChinaDreams:lint kangcunhua$ go install

验证依赖包安装结果

切回vsc,源码界面保存,世界清静了

Finished running tool: /usr/local/go/bin/go vet ./...Finished running tool: /usr/local/go/bin/go build -i -o /var/folders/0_/rnwscjh51j10hpqykp0rdly00000gp/T/go-code-check opsdev.ren/hellomath/mathapp

安装项目调试插件

推荐brew安装,不用自己配置很多麻烦的东西:

brew install go-delve/delve/delve

安装报错

security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

解决办法

执行脚本导入证书。参考: mac上 go-delve 安装出现The specified item could not be found in the keychain 解决方法

ChinaDreams:~ kangcunhua$ cd $HOME/Library/Caches/Homebrew
ChinaDreams:Homebrew kangcunhua$ tar xf delve-*.gz # 解压delve包
ChinaDreams:Homebrew kangcunhua$ cd delve-1.0.0
ChinaDreams:delve-1.0.0 kangcunhua$ sh scripts/gencert.sh # 执行脚本导入证书
Password:
ChinaDreams:delve-1.0.0 kangcunhua$ brew install go-delve/delve/delve
ChinaDreams:delve-1.0.0 kangcunhua$ dlv version # 验证安装是否成功
Delve Debugger
Version: 1.0.0
Build: v1.0.0
ChinaDreams:delve-1.0.0 kangcunhua$ cd ..
ChinaDreams:Homebrew kangcunhua$ rm -f -R delve-1.0.0 # 清理工作

配置调试环境

配置lannch.json

{// 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "Launch","type": "go","request": "launch","mode": "debug","remotePath": "","port": 1024,"host": "127.0.0.1","program": "${fileDirname}","env": {},"args": [],"showLog": true}]
}

点F5调试,调试控制台输出如下:

2018/03/01 16:21:38 server.go:73: Using API v1
2018/03/01 16:21:38 debugger.go:98: launching process with args: [/Users/kangcunhua/Documents/work-space/go-project/src/opsdev.ren/hellomath/mathapp/debug]
API server listening at: 127.0.0.1:1024
2018/03/01 16:21:38 debugger.go:497: continuing
Hello,world. Sqrt(2) = 1.414213562373095

配置运行

关于 go run 、build、install的区别,请参考这里:go run。

按快捷键cmd + shift + B ,按提示:配置任务运行程序—> Others

新建task.json

{// See https://go.microsoft.com/fwlink/?LinkId=733558// for the documentation about the tasks.json format"version": "2.0.0","tasks": [{"label": "go-echo","type": "shell","command": "go","args": ["run","${file}"],"group": {"kind": "build","isDefault": true}}]
}

这时,对于可执行的程序文件,按快捷键cmd + shift + B,即可正常执行:

> Executing task in folder opsdev.ren: go run /Users/kangcunhua/Documents/work-space/go-project/src/opsdev.ren/hellomath/mathapp/main.go <Hello,world. Sqrt(2) = 1.414213562373095终端将被任务重用,按任意键关闭。

用户自定义

关于自动保存方面的个性配置,参考自这里,也可以看这里,有更详细的说明。

// 将设置放入此文件中以覆盖默认设置
{"workbench.activityBar.visible": true,"workbench.sideBar.location": "left",// 配置go开发环境 start"files.autoSave": "off","go.buildOnSave": true,"go.lintOnSave": true,"go.vetOnSave": true,"go.buildFlags": [],"go.lintFlags": [],"go.vetFlags": [],"go.coverOnSave": false,"go.useCodeSnippetsOnFunctionSuggest": false,"go.formatOnSave": true,"go.formatTool": "goreturns",//"go.goroot": "/usr/local/go",// 你的Goroot//"go.gopath": "/Users/kangcunhua/Documents/work-space/go-project",// 你的Gopath// 配置go开发环境 end
}

结束

截至目前,已经就go基础SDK、IDE调试、运行配置完毕。

开启你的基于Visio Studio Code的go开发IDE欢乐之旅吧!

Ps:后续研究下,如何使用远程调试,做一个开发机docker镜像出来,在团队作战中效率会高很多。

Tips1:关于移除git污点儿的实践

基础阅读

  • BFG Repo-Cleaner官网;
  • BFG作者访谈:使用Roberto Tyley的BFG Repo-Cleaner移除git库中的二进制文件
  • 如何清洗 Git Repo 代码仓库:这篇文章阐述了为什么要使用BFG ;
  • Git从库中移除已删除大文件:这篇文章阐述了传统命令式如何做的,实际上我尝试了下,未竟全功:历史commit的引用没删除掉,不知是不是使用有差错。不过文章阐述的排查大文件的知识点儿还是很有用的;命令例子如下:
git gc
git count-objects -v
git verify-pack -v .git/objects/pack/pack-8eaeb...9e.idx | sort -k 3 -n | tail -3
git rev-list --objects --all | grep 185ab8d
git log --pretty=oneline --branches -- spark-assembly-1.3.1-hadoop2.4.0.jar
git filter-branch --index-filter 'git rm --cached --ignore-unmatch  spark-assembly-1.3.1-hadoop2.4.0.jar' -- 646784d95f347749517a67c50c117f4bf85d0b42..
rm -Rf .git/refs/original
rm -Rf .git/logs/
git gc
git count-objects -v

安装java环境

工具基于Java运行。

ChinaDreams:Downloads kangcunhua$ brew cask install java8
....
==> installer: The install was successful.
?  java8 was successfully installed!
ChinaDreams:Downloads kangcunhua$ java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)

删除误提交的文件

ChinaDreams:Downloads kangcunhua$ java -jar bfg-1.13.0.jar --delete-files 201710-kangcunhua体检报告.pdf ../Documents/work-diary
ChinaDreams:Downloads kangcunhua$ cd ../Documents/work-diary
ChinaDreams:work-diary kangcunhua$ git reflog expire --expire=now --all && git gc --prune=now --aggressive

提交到github

ChinaDreams:work-diary kangcunhua$ git push
Everything up-to-date

查看了github,搞定。昨天尝试了各种办法,都没删掉的commit黑历史,终于清静了。据info对作者15年1月的访谈中,作者提到“基于下载量来做个估算,据此我猜测,BFG自发布以来,已经为大家节省了大约30人年的工作量。”

从我的尝试来看,速度和效率果然不是吹的,d=====( ̄▽ ̄*)b厉害。

Tips2:Homebrew:Mac缺失的软件包管理器

统一维护的软件仓库,丰富可信赖的软件源,替你搞定不少依赖问题。

用rock老师的话说,程序猿不用brew,就错过了mac系统太多的精彩。

安装:官方教程

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

检查版本

ChinaDreams:local kangcunhua$ brew -v
Homebrew 1.5.5
Homebrew/homebrew-core (git revision edac; last commit 2018-02-27)

卸载

实在搞不定brew问题时,可以使用重装大法。卸载之前,可以看看官方文档;

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

安装:国内教程

使用官方教程安装都特别慢时,可以参考这篇教程:Mac下使用国内镜像安装Homebrew

  • 由于官方弃用了旧的homebrew仓库,择期删除(目前国内镜像已删)
  • 将homebrew程序与软件包拆分成了两个仓库(brew.git与homebrew-core.git)

brew使用

日常使用可以用:

  • brew config 查看配置
  • brew doctor 排查有问题
  • brew help 查看使用教程

替换为中科大的源

最新教程参见官方链接

替换brew.git:
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git替换homebrew-core.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

配置二进制源

最新教程参见官方链接

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

注意坑

所有网上教程修改源有指向国内homebrew.git镜像的(比如https://git.coding.net/homebrew/homebrew.git),均已过期,会和新版本冲突;不建议尝试。我之前使用的这个镜像,但是brew升级到了新版本,导致冲突,一直报错,找不到origin,尝试修复很久都没搞定。最后只能祭出重装大法。

ChinaDreams:tt kangcunhua$ brew config
HOMEBREW_VERSION: 1.5.5
ORIGIN: (none) # 找不到origin
HEAD: (none)
Last commit: never
Core tap ORIGIN: (none)
Core tap HEAD: (none)
Core tap last commit: never
...
ChinaDreams:tt kangcunhua$ brew doctor
....
Warning: Missing Homebrew/brew git origin remote.Without a correctly configured origin, Homebrew won't update
properly. You can solve this by adding the Homebrew remote:git -C "/usr/local/Homebrew" remote add origin https://github.com/Homebrew/brew.git
.... # 但是执行此命令会提示 已经存在origin

Tips3:node.js卸载方法收集

引用自这里。

brew的安装方式

brew uninstall nodejs

官网下载pkg安装包的

sudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node.*}

​ 之前测试VUE前端框架,安装过node.js,还降级过版本。这次修复完brew后,发现通过brew安装的新版本不起作用,node -v还是执行旧版本。全卸载了,需要的时候,通过brew安装吧。

参考&&引用

  • Golang 在mac上用VSCode开发、Delve调试
  • Go开发:Mac上安装Go环境和VS Code
  • Visual Studio Code折腾记
  • VS Code开发技巧集锦
  • Visual Studio Code 配置指南
  • 用VSCode写python的正确姿势
  • go get golang.org/x 包失败解决方法

工具相关

  • 使用Homebrew配置Java开发环境
  • 使用BFG移除git库中的大文件或污点提交
  • Git 远程仓库 git remote
    • 你可能想要把你的本地的git库,既push到github上,又push到开源中国的Git@OSC上,怎么解决呢
    • git的一个远程库 可以对应多个地址
    • 首先,先增加第一个地址 git remote add origin <url1>
    • 然后增加第二个地址 git remote set-url --add origin <url2>
    • git push origin master 就可以一次性push到3各库里面了(使用git push也可)

转载于:https://my.oschina.net/hexie/blog/1627888

基于Visio Studio Code打造go的IDE相关推荐

  1. vue项目配置eslint(附visio studio code配置)

    eslint基础环境搭建 全局安装eslint:npm install eslint -g 项目eslint初始化:eslint --init,按团队或自己的编程风格回答三道题. ? How woul ...

  2. GitHub+jsDelivr+PicGo+Visual Studio Code 打造稳定、快速、高效、免费图床

    该文章的最新版本已迁移至个人博客[比特飞],单击链接:GitHub+jsDelivr+PicGo+Visual Studio Code 打造稳定.快速.高效.免费图床 | .Net中文网. 前言 本文 ...

  3. visio studio code安装之后的两个错误的解决办法

    安装visio studio Code出现的两个错误 1.^Error: Cannot find module 'c:/Users/Administrator/Desktop/����Ŀ¼/Micro ...

  4. 基于Visual Studio Code 的佳明穿戴设备APP开发平台的搭建

    本文以图文方式详细介绍了基于Visual Studio Code软件的佳明穿戴设备APP开发平台的搭建步骤.环境变量设置方法以及平台运行环境的设置等,为佳明穿戴设备APP的开发者提供了详尽的教程和参考 ...

  5. ESP32学习(1):ESP-IDF基于Visual Studio Code环境

    目录 [一]安装开发编译环境Visual Studio Code [1]先到乐鑫官网下载 [2]解压安装 [二]安装 ESP-IDF VS Code 插件 [1]安装扩展platformIO IDE ...

  6. PlatformIO平台下Arduino的开发过程-基于Visual Studio Code平台

    一.平台背景与关联 如果你是初学者建议阅读这一章节,搞清楚平台的结构对于开发而言有事半功倍的效果 (1)什么是Arduino? Arduino是一家制作开源硬件和软件的公司,同时兼有项目和用户社区,该 ...

  7. 基于Mono和VSCode打造轻量级跨平台IDE

      最近Visual Studio推出Mac版本的消息迅速在技术圈里刷屏,当工程师们最喜欢的笔记本电脑Mac,邂逅地球上最强大的集成开发环境Visual Studio的时候,会碰撞出怎样精彩的火花呢? ...

  8. Visio Studio Code 图标美化

    1.点击左侧导航栏最下面的带时钟表示的图标 进入软件商店,输入vscode-icon搜索下载第一项就可以 2.下载完成后自动完成安装,切换回project选项后,可以直接看到图标美化的效果

  9. office插件开发_Visual Studio Code有哪些你常用的插件?

    如果发起一项调查"你最喜欢的开发工具是什么?" 我认为,在当下VS Code会成为很多开发者的选择之一. 不得不说,VS Code近几年大踏步的向前发展,已经使得它在很多方面已经取 ...

最新文章

  1. 跨平台PHP调试器设计及使用方法——协议解析
  2. FPGA中状态机的稳定性
  3. 服务化改造实践 | 如何在 Dubbo 中支持 REST
  4. java字符串是不是整数的函数_java判断字符串是否为整数的方法
  5. 软件架构设计的六大原则
  6. 针对敲诈病毒(WanaCrypt0r2.0)的应对方案
  7. Unity3D切换场景之后光源失效(物体变暗)问题
  8. 技能Get·手动更新HP笔记本BIOS过程记录
  9. arpspoof: libnet_check_iface() ioctl: No such device 解决方法
  10. labview 编程样式_LabVIEW编程样式规则
  11. 不忘初心,能偷懒就偷懒:C#操作Word文件
  12. 网络模拟环境 NS-2仿真软件简介
  13. CSS二(复合选择器)
  14. GEE(8):使用MODIS填补由去云后的Landsat影像计算得到的NDVI数据
  15. Java基础读书笔记
  16. 豆腐西施新传:大学毕业生也去磨豆腐
  17. net framework 4.0 4.6.1
  18. [goa]golang微服务框架学习--安装使用
  19. 菜刀工具连接不上_win7连接打印机提示错误0x00000002的解决教程
  20. 编程语言进化史《禅与计算机程序设计艺术》 / 陈光剑

热门文章

  1. js日历控件html,jQuery日历插件sys-calendar.js
  2. URL伪静态。SEO中理解伪静态
  3. const*与*const的区别
  4. 利用frm和idb文件恢复mysql数据
  5. R7 5800H和i5 11300H参数对比差距大不大
  6. 2022 百度面试题
  7. Linux上重置MySQL密码 错误 ERROR 1045 (28000): Access denied for user ‘‘root‘‘@‘‘localhost‘‘ (using passw...
  8. 华为模拟器ensp安装与使用
  9. 简单工厂设计模式的好处
  10. Chap.15 总结《CL: An Introduction》 (Vyvyan Evans)