Go

官网:​​https://golang.org/​​​ 文档:​​https://golang.org/doc/​

开发环境配置

下载 Go:​​https://golang.org/dl/​

IDE :使用 GoLand is a cross-platform IDE built specially for Go developers。​​https://www.jetbrains.com/go/​

go 命令说明:

go
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 add dependencies to current module and install them
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:

buildconstraint build constraints
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
packages package lists and patterns
private configuration for downloading non-public code
testflag testing flags
testfunc testing functions
vcs controlling version control with GOVCS

Use "go help <topic>" for more information about that topic.

Hello World

源代码文件 hello_world.go :

package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
}

命令行运行:

go run hello_world.go

输出:

Hello, World!

在 GoLang IDE 中效果如下:


Go Quick Start 极简教程 / 陈光剑_编程语言


Go 语言简介

概述

Go 是一个开源的编程语言,它能让构造简单、可靠且高效的软件变得容易。

Go是从2007年末由Robert Griesemer, Rob Pike, Ken Thompson主持开发,后来还加入了Ian Lance Taylor, Russ Cox等人,并最终于2009年11月开源,在2012年早些时候发布了Go 1稳定版本。现在Go的开发已经是完全开放的,并且拥有一个活跃的社区。

Go 语言特色

简洁、快速、安全
并行、有趣、开源
内存管理、数组安全、编译迅速

Go 语言用途

Go 语言被设计成一门应用于搭载 Web 服务器,存储集群或类似用途的巨型中央服务器的系统编程语言。

对于高性能分布式系统领域而言,Go 语言无疑比大多数其它语言有着更高的开发效率。它提供了海量并行的支持,这对于游戏服务端的开发而言是再好不过了。

数据类型

Go 有一个相当简单的类型系统:没有子类型(但有类型转换),没有泛型,没有多态函数,只有一些基本的类型:

基本类型:int、int64、int8、uint、float32、float64 等
struct: 结构体
interface:一组方法的集合
map[K, V]:一个从键类型到值类型的映射
[number]Type:一些 Type 类型的元素组成的数组
[]Type:某种类型的切片(具有长度和功能的数组的指针)
chan Type:一个线程安全的队列
指针 *T 指向其他类型
函数:
具名类型:可能具有关联方法的其他类型的别名(LCTT 译注:这里的别名并非指 Go 1.9 中的新特性“类型别名”):

type T struct { foo int }
type T *T
type T OtherNamedType

具名类型完全不同于它们的底层类型,所以你不能让它们互相赋值,但一些操作符,例如 +,能够处理同一底层数值类型的具名类型对象们(所以你可以在上面的示例中把两个 T 加起来)。

映射、切片和信道是类似于引用的类型——它们实际上是包含指针的结构。包括数组(具有固定长度并可被拷贝)在内的其他类型则是值传递(拷贝)。

函数

package main

import "fmt"

func main() {
fmt.Println(sum(1, 1))
}

func sum(a int, b int) interface{} {
return a + b
}

分支

package main

import "fmt"

func main() {
fmt.Println(max(1, 2))
}

func max(a int, b int) interface{} {
var max int = 0
if a > b {
max = a
} else {
max = b
}
return max
}

循环

package main

import "fmt"

func main() {
fmt.Println(sum1_n(100))
}

func sum1_n(n int) interface{} {
var sum = 0
for i := 1; i <= n; i++ {
sum += i
}
return sum
}

数组

package main

import "fmt"

func main() {
var a []int = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
fmt.Println(sumArray(a))
}

func sumArray(a []int) interface{} {
var sum = 0
for i := 0; i < len(a); i++ {
sum += a[i]
}
return sum
}

指针

package main

import "fmt"

func main() {
var a int = 20
/* 声明指针变量 */
var ip *int
fmt.Println(ip==nil)
/* 指针变量的存储地址 */
ip = &a
fmt.Println(ip==nil)
fmt.Printf("a 变量的地址是: %x\n", &a)
/* 指针变量的存储地址 */
fmt.Printf("ip 变量储存的指针地址: %x\n", ip)
/* 使用指针访问值 */
fmt.Printf("*ip 变量的值: %d\n", *ip)
}

//true
//false
//a 变量的地址是: c0000ae008
//ip 变量储存的指针地址: c0000ae008
//*ip 变量的值: 20

集合类

文件 IO

生态

参考资料

​https://www.runoob.com/go/go-environment.html​​​​https://golang.org/cmd/go/​​​​https://golang.org/doc/​