如果遇到其它好的操作,会更新该文,更新时间:2022-12-07
为何写起这篇文章,是因为我一直以来使用的是 Goland 编辑器,但是它是收费,说句 “不要脸” 的话,我用的是破解的。
但知道这不好,所以就想试试看 VScode 也能否类似 Goland 的一样,使用起来很舒适。
新的选择
这是最新追加的内容,如果不想搞这么多配置,可以考虑 JetBrains Fleet,名字有点熟悉吧,免费的。
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Fd54b0f25-9426-4b83-9c51-2e6165a030b2%2F5024b8cc-2fdd-4d4c-93ff-74ba7b83be55%2FUntitled.png%3Fid%3Dfead7c4c-8b58-4fff-8183-f3db9939aeb6%26table%3Dblock%26spaceId%3Dd54b0f25-9426-4b83-9c51-2e6165a030b2%26expirationTimestamp%3D1722139200000%26signature%3D3Td92gy7uukIzA3R9-s3FRksIEYnwSMbnmsHtIR9TTM?table=block&id=fead7c4c-8b58-4fff-8183-f3db9939aeb6&cache=v2)
前提
- Go - 2. 环境搭建
- VScode
插件
这是 VScode 所需要的安装的插件,我会说明每个插件的作用。
- gopls,提示安装即可:VSCode 建议你启用 gopls,它到底是个什么东东? - 墨天轮
- VScode 提示 Analysis Tools Missing 时,进行安装。
- staticcheck静态检测
- dlv 调试工具
- (可选)wsl:windows环境下体验 Linux 开发环境。
使用
Debug
- 想 debug 的行前点击,如下图:
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Fd54b0f25-9426-4b83-9c51-2e6165a030b2%2F8c3437a9-4b5b-4cdf-9c9d-f4209a9550f6%2FUntitled.png%3Fid%3Dd91b11d1-97b3-4d63-965b-77f82cb2ac99%26table%3Dblock%26spaceId%3Dd54b0f25-9426-4b83-9c51-2e6165a030b2%26expirationTimestamp%3D1722139200000%26signature%3DUByrQAT5SO7HYhgM6v6ffSlgBus_Zhib7ZcmrSumZLA?table=block&id=d91b11d1-97b3-4d63-965b-77f82cb2ac99&cache=v2)
- 点击左侧 Debug。
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Fd54b0f25-9426-4b83-9c51-2e6165a030b2%2Fab051dcc-604a-4d70-8242-299544a75d8b%2FUntitled.png%3Fid%3Dcb322e45-0ae7-48eb-9525-e8c0dd22df1d%26table%3Dblock%26spaceId%3Dd54b0f25-9426-4b83-9c51-2e6165a030b2%26expirationTimestamp%3D1722139200000%26signature%3DSd31Gbbv4UTSKVC3ETfCpZgTJSj9Yj846oc0JcnlbiQ?table=block&id=cb322e45-0ae7-48eb-9525-e8c0dd22df1d&cache=v2)
开启自动保存
当文件修改后,默认是不会自动保存的。
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Fd54b0f25-9426-4b83-9c51-2e6165a030b2%2F319dd93f-f6d2-4d31-9b37-6e4c2fb07017%2FUntitled.png%3Fid%3D8f58c2ac-09ed-493c-9df3-db7e6d6dc0c8%26table%3Dblock%26spaceId%3Dd54b0f25-9426-4b83-9c51-2e6165a030b2%26expirationTimestamp%3D1722139200000%26signature%3Dw45s5E7GPBwSMi4-KY9vUdKFqdNJpPBul4_tNX1x92g?table=block&id=8f58c2ac-09ed-493c-9df3-db7e6d6dc0c8&cache=v2)
生成接口实现
安装 impl 工具,安装方法:快捷方式 → 查看教程。
![notion image](https://www.notion.so/image/https%3A%2F%2Fgithub.com%2Fgolang%2Fvscode-go%2Fraw%2Fmaster%2Fdocs%2Fimages%2Fgenerateinterfaceimplementation.gif?table=block&id=d9917111-475d-487b-953b-d23f8c1e0cd6&cache=v2)
自动生成单元测试
安装 gotests,安装方法:在一个要生成单元测试的方法或函数处右键点击
Go: Generate Unit Tests For Function
,如果没有安装 gotests,会提示你安装。![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Fd54b0f25-9426-4b83-9c51-2e6165a030b2%2F59a17127-230a-453a-a953-00324c716114%2FUntitled.png%3Fid%3D86903243-f550-49a8-8d7c-3cde5c95b0cf%26table%3Dblock%26spaceId%3Dd54b0f25-9426-4b83-9c51-2e6165a030b2%26expirationTimestamp%3D1722139200000%26signature%3DBExYK8GmqiWApcL5LntyRJfGv42hpHx_hkyUeynyUMw?table=block&id=86903243-f550-49a8-8d7c-3cde5c95b0cf&cache=v2)
补充:
生成好单元测试后,如果在单元测试方法上方点击
run test
运行时,其中的 t.Log
函数时,默认是不会进行输出的。![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Fd54b0f25-9426-4b83-9c51-2e6165a030b2%2F3c195ba3-7b67-4be5-8bc1-daf9532d85a6%2FUntitled.png%3Fid%3Ded8b4d7d-dc41-4771-b4a3-7607478fecff%26table%3Dblock%26spaceId%3Dd54b0f25-9426-4b83-9c51-2e6165a030b2%26expirationTimestamp%3D1722139200000%26signature%3DpCipwf99-joO0X0CsdXmLIEhP7QruSTPotY2Q-1gbIg?table=block&id=ed8b4d7d-dc41-4771-b4a3-7607478fecff&cache=v2)
原理是还是帮你执行了 go test 命令,只需要跟上 -v 参数即可,如何配置呢,如下:
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Fd54b0f25-9426-4b83-9c51-2e6165a030b2%2F644f2aca-2c69-4752-9df0-fa1804a4f16b%2FUntitled.png%3Fid%3D0e396d3b-29c5-49cc-8966-2d29a2844ede%26table%3Dblock%26spaceId%3Dd54b0f25-9426-4b83-9c51-2e6165a030b2%26expirationTimestamp%3D1722139200000%26signature%3DecuJNp237hJp9z7T98ZXdDMmHCYNatkx5k3un9gpKcU?table=block&id=0e396d3b-29c5-49cc-8966-2d29a2844ede&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Fd54b0f25-9426-4b83-9c51-2e6165a030b2%2F4eb2a78d-975f-4748-bd46-47eff837471f%2FUntitled.png%3Fid%3D3bbb999e-77d8-4e62-8150-194f94167592%26table%3Dblock%26spaceId%3Dd54b0f25-9426-4b83-9c51-2e6165a030b2%26expirationTimestamp%3D1722139200000%26signature%3DvR0t0a7K1Ca2KOf2906dKPXk1heT0nJg9HPsxHnT6WI?table=block&id=3bbb999e-77d8-4e62-8150-194f94167592&cache=v2)
自动填充结构体字段或删除
在初始化结构体时,在需要给所有字段赋值时,可以将所有字段进行自动填充。
打开命令面板 → Go: Fill struct,第一次的时候会提示你安装 fillstruct 工具。
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Fd54b0f25-9426-4b83-9c51-2e6165a030b2%2Fcd6cc70b-709f-4feb-9ac3-2ae084c2e80f%2FUntitled.png%3Fid%3De6fc83e8-5423-49fc-80bc-d9036708ca73%26table%3Dblock%26spaceId%3Dd54b0f25-9426-4b83-9c51-2e6165a030b2%26expirationTimestamp%3D1722139200000%26signature%3D_B9Ubsevd1IAqja8tQB2RqbiAaUyZ_SGxR5WKY2V6V4?table=block&id=e6fc83e8-5423-49fc-80bc-d9036708ca73&cache=v2)
删除如下图:
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Fd54b0f25-9426-4b83-9c51-2e6165a030b2%2F9730ff70-1395-4232-a1d5-1f28fe8e79eb%2FUntitled.png%3Fid%3Ddf6a6d2b-fc90-4320-aaea-9cd9fcbee060%26table%3Dblock%26spaceId%3Dd54b0f25-9426-4b83-9c51-2e6165a030b2%26expirationTimestamp%3D1722139200000%26signature%3Dpo3TZ0OBczr7OD71bfZPQGN_lz_MSiBA3Hprd-7rTVc?table=block&id=df6a6d2b-fc90-4320-aaea-9cd9fcbee060&cache=v2)
自动给结构体增加 tag
打开命令面板,点击如下图命令,如果第一次会提示安装 gomodifytag 工具。
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Fd54b0f25-9426-4b83-9c51-2e6165a030b2%2F090f74af-736f-4231-8aa7-598c9b5c7d80%2FUntitled.png%3Fid%3Dee891794-a32e-4cce-9955-605986478683%26table%3Dblock%26spaceId%3Dd54b0f25-9426-4b83-9c51-2e6165a030b2%26expirationTimestamp%3D1722139200000%26signature%3DKYmzAlc-1pnvBDcBDJZromMV98LeJW05KpyvQS37HVQ?table=block&id=ee891794-a32e-4cce-9955-605986478683&cache=v2)
默认的生成结果,如下所示:
type Student struct { Name string `json:"name,omitempty"` Height float64 `json:"height,omitempty"` }
如果想自定义,则按下图打开设置,对于我,我不想增加 omitempty,则可进行删除。
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Fd54b0f25-9426-4b83-9c51-2e6165a030b2%2F4469d21e-7ee0-480c-85d3-ecafd77318f4%2FUntitled.png%3Fid%3D374d28f2-db1f-43d9-8b16-004220f63e7a%26table%3Dblock%26spaceId%3Dd54b0f25-9426-4b83-9c51-2e6165a030b2%26expirationTimestamp%3D1722139200000%26signature%3DNmYlNHZH8vJpScyRIxzfQsUD51Ak41uPzie4UykHTMc?table=block&id=374d28f2-db1f-43d9-8b16-004220f63e7a&cache=v2)
transform(命名方式):
- snakecase(默认)表示下划线形式,如:snake_case
- camelcase 驼峰命名,如:camelCase
tags(标签名,多个以逗号分隔):json,xml
重构函数
选中所有重构的部分,打开命令面板,输入下图,第一次提示安装 godoctor。
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Fd54b0f25-9426-4b83-9c51-2e6165a030b2%2F758c54f7-29fe-44af-a8c9-5736e13b261f%2FUntitled.png%3Fid%3D09658233-6f21-4238-957c-60c9ab16538c%26table%3Dblock%26spaceId%3Dd54b0f25-9426-4b83-9c51-2e6165a030b2%26expirationTimestamp%3D1722139200000%26signature%3D3EcQekWHf6plXHrTq3mKICRCkTbxwBEGiD6jwaGrcmU?table=block&id=09658233-6f21-4238-957c-60c9ab16538c&cache=v2)
快捷方式
内容太多,我独立成单独一篇文章:
VScode 提高效率的操作VScode 优点
- 免费
- 登陆 Github 账号,同步配置
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Fd54b0f25-9426-4b83-9c51-2e6165a030b2%2Fdcbc21a1-a90c-418e-9f7a-001a7bb857c8%2FUntitled.png%3Fid%3D67877792-a846-4ea8-a983-6a203ce2335c%26table%3Dblock%26spaceId%3Dd54b0f25-9426-4b83-9c51-2e6165a030b2%26expirationTimestamp%3D1722139200000%26signature%3DwjvraEfinvdqeubImj5GQMXFFTHcEOYUnXLW_VkjRoY?table=block&id=67877792-a846-4ea8-a983-6a203ce2335c&cache=v2)