My full-stack basis of the language of the road -Go Go language development environment to build

My full-stack basis of the language of the road -Go Go language development environment to build

2.1 Go language compiler download

Before developing a program Go language, first Quguan network golang.google.cn download Go language compiler, the compiler of the role is to program developers to use Go Go Go compiler for the language written in compiled into machine instructions corresponding to the platform. Because the computer does not recognize the program developer-written, it needs Go compiler to compile the source code into binary machine instructions a computer identified.
golang official website provides Windows, Linux, Go language compiler macOS version of the download When you click Download Go, will jump to the various versions of the Go language compiler download, students can choose the corresponding for its own operating system, compiler version Go If macOS can choose to download go1.12.9.darwin-amd64.pkg file when Chrome will automatically download the compiler go after clicking the download link later when the download is complete Chrome will prompt this type of file can harm your computer, here choose to keep and still retain files downloaded
golang 官 网

Go various versions of the compiler

Go compiler version macOS

go1.12.9.darwin-amd64.pkg

Retention
Any natural reservations

2.2 Go language compiler installed

After the download is complete Go language compiler can be installed, previously downloaded directly here to open in the Chrome browser installation files go1.12.9.darwin-amd64.pkg After selecting the type of installation, the system prompts for a password, if there is TouchID, TouchID directly to installation in a successful installation when Close the window, the system will prompt "Go installer you want to remove the trash it?" If you want to keep clicking reservations to, or remove to paper baskets, next time you go to the official website to download to install.
Welcome to Go installer

Installation Type

installing

Successful installation

Enter the go version can use terminal software (eg iTerm) After installation is complete view Go compiler version information
go version

如果想要查看Go语言所有相关的环境变量,可以使用go env命令查看
go env
其中我们需要关注的是GOPATH和GOROOT,其中GOROOT是go语言编译器的安装路径,GOPATH是开发人员编写Go程序的工作空间,也就是存放Go代码的地方,普通用户(例如liuguanglei)登录默认的值是/Users/liuguanglei/go,如果是root用户登录,默认的GOPATH是/var/root/go,这也就意味着Go编译器编译时默认会在该路径下查找Go源程序,这里通过修改/etc/profile文件实现自定义GOPATH。

首先使用su root 命令并输入密码切换到root用户下
su root

然后使用vim命令编译/etc目录下的profile文件

sh-3.2# vim /etc/profile

此时vim处于阅读模式,如果想要修改profile文件的内容,按键 "i"便可进入编辑模式
然后添加GOPATH的环境变量配置

export GOPATH=/Users/liuguanglei/Documents/workspace/go
export PATH=$PATH:$GOPATH

添加完成以后,按esc退出编辑模式,然后输入wq!保存之前的修改,此时添加的GOPATH环境变量实际上还没有生效,使用source命令让之前的修改生效即可

sh-3.2# source /etc/profile

GOPATH Configuration
GOPATH配置

然后无论是普通用户登录还是root用户登录,使用go env GOPATH查看环境变量都是在/Users/liuguanglei/Documents/workspace/go目录下
GOPATH global environment variable configuration

2.3 macOS安装Visual Studio Code

在安装Go编译器并配置好GOPATH之后,便可以进行Go程序的开发了。
Go源程序可以使用任意的文本编辑器开发,我这里采用Visual Studio Code来开发Go程序

首先去Visual Studio Code的官网下载并安装,官网提供了各个操作系统的版本下载。
Official website

After you download the package will extract the Visual Studio Code VSCode-darwin-stable.zip drag and drop "Applications" folder to use.

Download VisualStudio Code, macOS the default path is "download"
download

Decompression VSCode-darwin-stable.zip drag and drop Visual Studio Code to the Applications folder
Decompression

application

After the installation is complete, you can launch the Visual Studio Code from LaunchPad Visual Studio Code startup interface
LaunchPad

Visual Studio Code startup interface

2.4 Go Language HelloWorld

After starting the Visual Studio Code, GOPATH corresponding directory before the first open and then write the first Go program in the catalog: helloworld.go
Open Folder
Gofatः

package main
import "fmt"

func main(){
    fmt.Println("macOS & Go 1.12.9 & Visual Studo Code")
}

After the completion of the preparation can be used to compile the program go build on the terminal

liuguangleideMacBook-Pro:~ liuguanglei$ cd Documents/workspace/go/
liuguangleideMacBook-Pro:go liuguanglei$ go build helloworld.go
liuguangleideMacBook-Pro:go liuguanglei$ ./helloworld
macOS & Go 1.12.9 & Visual Studo Code

go build
go build

helloworld.go compile source files generated by default, and the same name as the executable file helloworld, then you can run compiled using ./helloworld

Can also be used directly go run commands directly run the source file, you can execute the command does not generate a file helloworld

liuguangleideMacBook-Pro:go liuguanglei$ go run helloworld.go
macOS & Go 1.12.9 & Visual Studo Code

go build
go build

2.5 Visual Studio Code integrated plug-Go

When written using Visual Studio Code Go plug-ins are not installed, source file, write code is not smart tips, but also need to compile the Go program through an external terminal program (such as iTerm).

First, switch to the Visual Studio Code of extensions view (shortcut Command + Shift + x), and then search in the search bar plug-in installation to go. After the plug-in installation is complete, the code will be written in Go the smart tips.
Search Go plug-ins installed

The installation is complete

And visual studio code comes with the terminal, directly to compile the Go program that comes with the terminal.
The use of plug-in programs written in Go, and use the built-in terminal to compile and run

Go 2.6 GoLand development program

Introduction 2.6.1 GoLand

GoLand是Jetbrains公司推出专为Go开发人员构建的跨平台IDE,可以运行在Windows,Linux,macOS系统之上,除了核心Go开发之外,它还支持JavaScript,TypeScript,NodeJS,SQL,数据库,Docker,Kubernetes和Terraform。总而言之,这些功能使其完美地适用于任何任务,无论是现代Web应用程序还是DevOps工具。

GoLand为Git,GitHub和Mercurial提供开箱即用的支持。用户可安装的插件支持Perforce,ClearCase和其他插件。

GoLand丰富的生态系统包括1000多个插件,可用于根据您的特定需求定制IDE。根据您的喜好自定义,扩展和更改所有内容:从添加对Angular和Vue.js项目的支持,或获取Nyan Cat进度条,到设置完全不同的IDE主题。

2.6.2 GoLand安装

首先去官网下载GoLand
GoLand
当点击下载之后,网站会检测当前浏览器用户对应的操作系统版本,然后下载对应操作系统版本的GoLand,目前最新版本的GoLand是2019.2.1。
Download GoLand

下载完成之后便可以进行安装了,点击下载的goland-2019.2.1.dmg文件
Download completed
然后将GoLand拖放到Applications文件夹即可。
The GoLand drag to your Applications folder

如果想要完全卸载GoLand,可以删除如下的目录

~/Library/Preferences/
~/Library/Caches/
~/Library/Application Support/
~/Library/Logs/

2.6.3 GoLand首次启动设置

首先从启动台中启动GoLand
Start GoLand

导入GoLand设置从其他来源
如果之前安装过其他版本的GoLand,可以选择Config or installation folder,选择其他版本的GoLand配置。
我这里已经把之前安装的GoLand完全删除了,所以选择Do not import settings
Import GoLand Settings From...

因为GoLand是收费的IDE,同时也提供了30天免费试用的方式。如果经济能力允许的话,可以从指定渠道购买正版GoLand.
GoLand提供了Jetbrains Account,Activition Code和License Server三种激活方式,使用前必须激活或者选择免费试用
Activation GoLand

当激活或者选择免费试用之后便会启动GoLand
GoLand activation

2.7 GoLand开发Go程序

当GoLand启动后,便可以使用它来编写Go程序了。

首先选择New Project创建一个项目
New Project

设置项目路径和GOROOT
Setting the project path and GOROOT

GoLand会自动创建项目
go-core-programming
创建Go File
Creating Go File
创建helloworld.go,选择Simple Applciaiton
Create a Simple Application

编写helloworld.go

package main

import "fmt"

func main() {
    fmt.Println("mac0S10.14.6 & Go1.12.9 & GoLand 2019.2.1 Hello World")
    
}

程序说明

  • package main 表示定义一个包,main是可执行程序的包名,所有的Go源文件头部必须要要有一个包声明语句,Go通过包来管理命名空间
  • import "fmt"表示引用一个外部的包fmt,import可以是标准库的包,也可以使第三方或者自定义的包,fmt是标准输入输出包
  • func关键字定义一个函数,函数名为main,main代表Go程序的入口
  • fmt.Println()是fmt包中的Println函数,函数的实参是一个字符串字面量,在标准输出(即控制台终端)上输出mac0S10.14.6 & Go1.12.9 & GoLand 2019.2.1 Hello World

Go源程序特征

  • 源程序以.go为后缀
  • 源程序默认为UTF-8编码
  • 标识符区分大小写
  • 语句结尾的分号可以省略
  • 函数以func开头,函数体开头的"{"必须在函数头所在行尾部,不能单独起一行
  • 字符串字面量使用""括起来
  • 调用包里面的方法通过包名.方法名实现,例如fmt.Println
  • main函数所在的包必须是main包

Go run the program
first run can click the icon to run the main method of operation, or the right mouse button Run Run.
Follow-up can click on the top right corner of the run to run.
Go run the program

If you like black theme, you can Command +, enter the setup, then you can set the Theme for the Darcula on Apperance options.
Set Theme

Guess you like

Origin www.cnblogs.com/ittimeline/p/11421095.html