Go语言
-
GO语言的优点
-
语法简单
import "fmt" var a, b = 1, 2 a, b = b, a fmt.Println(a,b)
-
静态数据类型、编译语言
a:=1 b:=false
-
内置支持并发
-
可以直接编译成机器码
-
-
下载与安装(注意不要安装在有中文的目录)
-
下载地址
官方网站: https://golang.google.cn/
中文网站: https://studygolang.com/
-
下载msi文件,目前最新版本为
1.14.1
,双击开始安装,这里我安装的位置是D
盘golang
目录下; -
配置环境变量:
- 打开计算机高级系统设置,找到环境变量,在系统环境变量中新建变量名
GOROOT
,值为刚才安装的目录D:golang
; - 然后点击系统环境变量中的
Path
,再点编辑,再点新建,里面输入:%GOROOT%in
; - 接着点确定、确定;
- 打开cmd,输入
go version
,如果正确显示了版本号,就表示环境变量配置成功,也可以用go env
;
- 打开计算机高级系统设置,找到环境变量,在系统环境变量中新建变量名
-
环境变量参数解释:
GOROOT
:表示go语言库的根目录完整路径;PATH
:path中配置内容方便在命令行快速调用go语言库中的工具;GOPATH
:指定了你的工作空间位置。 注意,它绝对不能和你的Go安装目录相同。
-
-
第一个go语言程序---hello world
在
D
盘go
目录下新建一个文件,文件名称为hello.go
,使用文本编辑器notepad++
或者vscode
打开,在里面输入下面的内容:(其中//
后面的内容是注释内容,不影响程序的运行)package main // 包名 import "fmt" // 引入go语言库中写好的包 func main(){ // 定义一个函数 fmt.Println("hello world") // 调用包 }
输入完成之后,在命令行找到
hello.go
文件所在的位置,然后输入go run hello.go
,如果能够显示hello world
,就表示正确运行了; -
注释
单行注释:
// 这里是单行注释
多行注释:
/* 这里是多行注释 多行注释1 多行注释2 */
-
package关键字
- package表示当前代码所属的包(可以理解为一种组织结构,比如你属于哪家公司),其他package通过包名调用这个包下的内容;
- package是必须有的,每个文件的package必须存在有效代码的第一行(除注释外);
package main
是程序入口包,这个包中可以编写主函数;
-
import 关键字
-
import表示引入包,引用其他包的内容;
-
import "fmt"
表示引用fmt
包;fmt
包是go语言库中自带的包,实现了输入输出等功能; -
import必须存在于package关键字下面,函数或变量声明的上面;
-
import导入包时,包名两侧必须使用双引号,支持一下集中语法:
// 一个一个包导入 import "fmt" import "os" // 一次导入多个包(官方推荐) import ( "fmt" "os" )
-
Go语言要求,导入的包都必须使用,否则编译出错,比如如果导入了os包,但是没有使用,会报如下错误信息:
imported and not used: "os"
-
-
main函数
-
func main
称为主函数,是整个程序的入口,最先执行主函数中的代码; -
main()后面的
{
必须和func
在同一行,否则运行时提示如下信息:.main.go:16:6:syntax error: unexpected semicolon or newline before {
-
一行语句结束,可以不用写分号
;
,但是写了也能正常运行,注:C/C++、Java如不写;
编译不通过;
-
一、解压版GO语言安装包中自带工具
-
%GOROOT%in
下包含的3个.exe
:- go.exe:编译、运行、构建等都会用到的主程序;
- godoc.exe:查看包或者函数的源码;
- gofmt.exe:格式化文件;
-
go.exe可选参数
Go is a tool for managing Go source code. Usage: go <command> [arguments] The commands are: bug start a bug report build compile packages and dependencies clean remove object files and cached files doc show documentation for package or symbol env print Go environment information fix update packages to use new APIs fmt gofmt (reformat) package sources generate generate Go files by processing source get download and install packages and dependencies install compile and install packages and dependencies list list packages or modules mod module maintenance run compile and run Go program test test packages tool run specified go tool version print Go version vet report likely mistakes in packages Use "go help <command>" for more information about a command. Additional help topics: buildmode build modes c calling between Go and C cache build and test caching environment environment variables filetype file types go.mod the go.mod file gopath GOPATH environment variable gopath-get legacy GOPATH go get goproxy module proxy protocol importpath import path syntax modules modules, module versions, and more module-get module-aware go get module-auth module authentication using go.sum module-private module configuration for non-public modules packages package lists and patterns testflag testing flags testfunc testing functions Use "go help <topic>" for more information about that topic.
-
常用参数
go version
,查看go语言版本;go env
,查看go语言详细环境;go list
,查看go语言文件目录;go build
,把源码文件构建成系统可执行的文件;go clean
,清空生成的可执行文件;go vet
,静态解析文件,检查是否有语法错误等;go get
,从远程下载第三方go语言库,默认会放在GOPATH
目录下;go bug
,提交bug;go test
,测试;go run
,运行文件;
二、打印输出
-
Fprint(),在Go web中使用比较多,把内容写入到响应流中;
源码如下:
// Fprint formats using the default formats for its operands and writes to w. // Spaces are added between operands when neither is a string. // It returns the number of bytes written and any write error encountered. func Fprint(w io.Writer, a ...interface{}) (n int, err error) { p := newPrinter() p.doPrint(a) n, err = w.Write(p.buf) p.free() return }
函数参数中的第一个参数是输出流,后面参数是内容,表示把内容写入到输出流中;第一个结果返回值表示输出内容的长度(字节数),第二个返回值表示错误,如果没有错误取值
nil
。Fprintln()输出后会添加换行符,所以长度比内多1个;Fprint支持三种方式:
- Fprint(os.Stdout,”内容1”), 向流中写入内容;
- Fprintln(os.Stdout, “内容2”),向流中写入内容之后额外写入换行符;
- Fprintf(os.Stdout, “%s”, “内容3”),根据verb格式向流中写入内容;
-
Print(),将内容写入到控制台中;
- Print(“内容1”, “内容2”),输出的内容不换行;
- Println(“内容1”, “内容2”),输出的内容自动换行;
- Printf(“verb”, “内容”),根据verb输出指定格式内容;
-
Sprint(),将内容转成string类型返回,需要变量接收;
三、转义字符
-
在go语言中,转义字符又称
verb
,%
和,能与其他字符组合以表示特殊含义;转义字符在输入和输出语句中使用比较频繁:
fmt.Printf("verb", "内容") // 输出 fmt.Scanf("verb", "接收变量") // 输入
示例:
/* %d 十进制数 %x 小写字母十六进制数 %X 大写字母十六进制数 %o 八进制数 %b 二进制数 %f %g %e 浮点数 %t 布尔值 %c 字符 %s 字符串 %q 带双引号字符串 %v 内置格式内容 %T 类型 %p 内存地址 %% 字符% 换行 制表符 */ fmt.Printf("%d", 19) // 19 fmt.Printf("%x %X", 12, 13) // c D fmt.Printf("%c", 65) // C fmt.Printf("%c", 97) // c fmt.Printf("%t", false) // false fmt.Printf("%f", 1.6) // 1.6 fmt.Printf("%s %q", "hello", "hello") // hello "hello" // 获取内存地址 v := 5 fmt.Printf("%p", &v) // 0xc00000a100 fmt.Printf(" 增长率为%d%%", 20) // 增长率为20%