Windows10上基于Visual Studio Code安装Golang开发环境

时间:2023-03-09 00:51:12
Windows10上基于Visual Studio Code安装Golang开发环境

GoLang简介

Windows10上基于Visual Studio Code安装Golang开发环境

Go编程语言是一个开源项目,它使程序员更具生产力。

Go语言具有很强的表达能力,它简洁、清晰而高效。得益于其并发机制,用它编写的程序能够非常有效地利用多核与联网的计算机,其新颖的类型系统则使程序结构变得灵活而模块化。 Go代码编译成机器码不仅非常迅速,还具有方便的垃圾收集机制和强大的运行时反射机制。 它是一个快速的、静态类型的编译型语言,感觉却像动态类型的解释型语言。

Go is a new language. Although it borrows ideas from existing languages, it has unusual properties that make effective Go programs different in character from programs written in its relatives. A straightforward translation of a C++ or Java program into Go is unlikely to produce a satisfactory result—Java programs are written in Java, not Go. On the other hand, thinking about the problem from a Go perspective could produce a successful but quite different program. In other words, to write Go well, it's important to understand its properties and idioms. It's also important to know the established conventions for programming in Go, such as naming, formatting, program construction, and so on, so that programs you write will be easy for other Go programmers to understand.

Go是一门新语言。尽管它借鉴了现有语言的思想,但它有一些不寻常的特性,使得有效的Go程序在性质上不同于用其亲属编写的程序。将C++或java程序直接翻译成GO不可能产生满意的结果。java程序是用java编写的,不是GO。另一方面,从Go的角度思考问题可以产生一个成功但完全不同的程序。换句话说,要想写得好,理解它的性质和习语是很重要的。同样重要的是要知道在Go中编程的既定约定,比如命名、格式化、程序构造等等,这样你编写的程序就可以让其他Go程序员很容易理解。

Windows安装

Windows10上基于Visual Studio Code安装Golang开发环境

直接前往谷歌中国的官网安装即可。

下载Msi文件之后,一路安装即可,保持默认设置。

Windows10上基于Visual Studio Code安装Golang开发环境

安装配置

检验安装

通过go关键词,带上Version命令即可在PS中查看到当前安装版本。如果上诉安装已经就位,那么就会成功打印相关版本。

go version

Windows10上基于Visual Studio Code安装Golang开发环境

检查环境变量

go env

Windows10上基于Visual Studio Code安装Golang开发环境

设置中国区代理(加速模块下载)

暂时用七牛云提供的国内代理,毕竟七牛算是国内Go的一大布道师公司,相信是靠谱的。

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct

Windows10上基于Visual Studio Code安装Golang开发环境

在VSC中开启Hello World

安装插件

在VSC的插件市场中,搜索关键词Go即可轻松找到官方插件。

Windows10上基于Visual Studio Code安装Golang开发环境

安装即可。

检查安装相关工具依赖

我们新建一个Hello-Go.go的文件,并且用VSC打开。

Windows10上基于Visual Studio Code安装Golang开发环境

这时候,VSC的右下角,会提示你要安装什么依赖,根据提示来操作即可。

Windows10上基于Visual Studio Code安装Golang开发环境

Windows10上基于Visual Studio Code安装Golang开发环境

根据提示呢,这个依赖工具被下载到了C:\Users\User\go\bin目录中,查看后,确实如此。

Windows10上基于Visual Studio Code安装Golang开发环境

Windows10上基于Visual Studio Code安装Golang开发环境

而且你会发现,当上面两个工具都被安装好之后,这时候VSC的Go下标状态也变了,已经不是之前的叹号了,而是一个闪电号。

在vsc中,如果启动Go的调式,你会发现,还依赖另外一个工具,根据提示安装即可。

Windows10上基于Visual Studio Code安装Golang开发环境

开始真正的Hello Wolrd

在当前代码的文件夹下,我们打开VSC的终端,输入下面的命令,新建一个Hello程序。

go mod init $modName

Windows10上基于Visual Studio Code安装Golang开发环境

它会自动创建一个名为go.mod的文件。

Windows10上基于Visual Studio Code安装Golang开发环境

简单来看,这里面应该是存了当前项目的依赖配置。

从配置看得出,依赖了go 1.16,并且有一个叫hello的模块。

接下来,我们只需要新建一个名为main.go的文件。

package main

import "fmt"

func main() {

	fmt.Println("hello world, see u go")
}

Windows10上基于Visual Studio Code安装Golang开发环境

然后,Ctrl+F5即可把它运行起来,并且在输出控制台,即可看到我们的运行结果。

Windows10上基于Visual Studio Code安装Golang开发环境

我们也可以在VSC中创建一个指定File的Launch.json配置。

{
"version": "0.2.0",
"configurations": [
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${file}"
}
]
}

接下来,便可选中某个Go文件,然后启动调试了。

Windows10上基于Visual Studio Code安装Golang开发环境

运行结果,一样的会打印在控制台。

Windows10上基于Visual Studio Code安装Golang开发环境

有意思的是,你会看到,它默认会选择一个端口号来运行。

命令行运行

如果你在想在命令行执行Go文件,也很简单,切换到Go文件所在位置,使用run命令即可。

go run $FileName

Windows10上基于Visual Studio Code安装Golang开发环境

学习资源