Go Quick Start 极简教程

Go

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

开发环境配置

下载 Go:
(官网)https://golang.org/dl/
(国内镜像)https://golang.google.cn/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 语言简介

概述

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

结构体 struct

package main

import "fmt"

func main() {
    var b = Book{
        "Go Programming",
        "ken",
        10000,
    }

    fmt.Println(b.title)
    fmt.Println(b.author)
    fmt.Println(b.id)
    //Go Programming
    //ken
    //10000
}

type Book struct {
    title  string
    author string
    id     int
}

集合 Map

Map 是一种无序的键值对的集合。Map 最重要的一点是通过 key 来快速检索数据,key 类似于索引,指向数据的值。

Map 是一种集合,所以我们可以像迭代数组和切片那样迭代它。不过,Map 是无序的,我们无法决定它的返回顺序,这是因为 Map 是使用 hash 表来实现的。

可以使用内建函数 make 也可以使用 map 关键字来定义 Map:

/* 声明变量,默认 map 是 nil */
var map_variable map[key_data_type]value_data_type

/* 使用 make 函数 */
map_variable := make(map[key_data_type]value_data_type)

如果不初始化 map,那么就会创建一个 nil map。nil map 不能用来存放键值对。

package main

import "fmt"

func main() {
    var ccmap = make(map[string]string)
    ccmap["China"] = "北京"
    ccmap["Italy"] = "罗马"
    ccmap["Germany"] = "柏林"
    for c := range ccmap {
        fmt.Println(c, "Capital is ", ccmap[c])
    }

    c, contains := ccmap["American"]
    if contains {
        fmt.Println("American's capital is ", c)
    } else {
        fmt.Println("Not exist")
    }
}

//Italy Capital is  罗马
//Germany Capital is  柏林
//China Capital is  北京
//Not exist

Go 优势与生态

Go 语言生态

当下使用 Go 语言开发的软件产品非常众多,而且知名的产品也不再少数,足见这门语言的强大:

软件 描述 链接
docker 家喻户晓的容器技术 github.com/moby/moby
kubernetes 容器编排引擎,google出品 github.com/kubernetes/kubernetes
etcd 分布式服务注册发现系统 github.com/etcd-io/etcd
influxdb 时序数据库 github.com/influxdata/influxdb
grafana 数据监控可视化看板 github.com/grafana/grafana
prometheus 开源监控系统 github.com/prometheus/prometheus
consul 分布式服务发现系统 github.com/hashicorp/consul
nsq 亿级消息队列 github.com/nsqio/nsq
TiDB 分布式数据库, go + rust 打造 github.com/pingcap/tidb

除了上述表格中列举的产品外, Go 语言还涉足于像区块链、微服务等场景,开源的框架也非常多,所以说 Go 语言是一门值得去学习的语言。

性能优势

Go 语言被称为是:"21世纪的C语言",虽然这个帽子戴的有点高,不妨这里给大家解读一下,其实这句话有两层含义:

第一层含义是: Go 语言的语法和C语言类似,如果你会C语言,上手会很快。但如果你不会C语言,其实也不用担心,比起C语言, Go 语言上手其实很简单。

第二层含义是: Go 语言的性能, Go 语言内置强大的并发模型 goroutine,它能让我们快速开发高并发web系统,并且在同样服务器资源的情况下, Go 语言表现出来的性能也是相当的优秀,这也是推荐大家选择选择 Go 语言的原因之一。

部署运维成本低

这是我选择 Go 语言的第二个原因。 Go 语言属于编译型语言,最终部署上线,我们只需要部署项目编译后的二进制文件即可,类比一下像 PHP 或 python 这种解释性语言在服务器上运行还需要安装相应的运行环境,而使用二进制代码方式使得部署变得更为简单,也不会存在多版本共享环境的兼容性问题,运维也变得非常容易。

编码格式统一

Go 语言官方内置了统一代码风格的工具 gofmt( IDE 一般都会内置集成), 用来规范大家代码风格,这对于需要多人协作项目尤为重要。

比如,现在你需要接手一个外包团队项目,这个外包项目如果项目的编码风格和你不一致,当你接手后,你的心情多半沮丧的,因为这会增加你的改造成本。

更或是,你现在需要接手一个由前前前同事所开发的老项目,如果代码风格不统一,可想而知接下来会发生什么...

Go 语言官方其实也是注意到了这些痛点,也避免了由第三方产生的规范不一致的问题,以官方的角度统一规范,从而降低整个项目的协作运维成本。

测试简单

Go 语言编写测试代码真的非常简单,这个特性真的是刚需啊。我们在项目开发中经常会遇到这样的事情,比如当你负责为项目负责开发一个独立的短信服务模块,这个功能供另个同事在项目里调用,当你开发完成后如何测试功能是否正常呢?

这种测试在 Go 语言中变得相当容易,你只需要在你代码文件同级目录创建一个以 _test.go 结尾的文件,然后在文件里编写针对特定功能测试函数即可,更重要的是,这个测试文件是可以单独运行的,你不需要再去集成整个项目运行环境, Go 语言让单元测试变得非常容易。

Go Projects

https://github.com/moby/moby
https://github.com/docker/docker-ce
https://github.com/kubernetes/kubernetes
https://github.com/etcd-io/etcd
https://github.com/gin-gonic/gin
https://github.com/hashicorp/consul
https://github.com/micro/go-micro
https://github.com/nsqio/nsq
https://github.com/elastic/beats
https://github.com/pingcap/tidb
https://github.com/CodisLabs/codis
https://github.com/baidu/bfe
https://github.com/caddyserver/caddy
https://github.com/cockroachdb/cockroach

参考资料

https://blog.csdn.net/javahongxi/article/details/109771251
https://www.runoob.com/go/go-environment.html
https://golang.org/cmd/go/
https://golang.org/doc/
https://www.cnblogs.com/xingxia/p/golang.html

PS:go内置类型

// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/*
    Package builtin provides documentation for Go's predeclared identifiers.
    The items documented here are not actually in package builtin
    but their descriptions here allow godoc to present documentation
    for the language's special identifiers.
*/
package builtin

// bool is the set of boolean values, true and false.
type bool bool

// true and false are the two untyped boolean values.
const (
    true  = 0 == 0 // Untyped bool.
    false = 0 != 0 // Untyped bool.
)

// uint8 is the set of all unsigned 8-bit integers.
// Range: 0 through 255.
type uint8 uint8

// uint16 is the set of all unsigned 16-bit integers.
// Range: 0 through 65535.
type uint16 uint16

// uint32 is the set of all unsigned 32-bit integers.
// Range: 0 through 4294967295.
type uint32 uint32

// uint64 is the set of all unsigned 64-bit integers.
// Range: 0 through 18446744073709551615.
type uint64 uint64

// int8 is the set of all signed 8-bit integers.
// Range: -128 through 127.
type int8 int8

// int16 is the set of all signed 16-bit integers.
// Range: -32768 through 32767.
type int16 int16

// int32 is the set of all signed 32-bit integers.
// Range: -2147483648 through 2147483647.
type int32 int32

// int64 is the set of all signed 64-bit integers.
// Range: -9223372036854775808 through 9223372036854775807.
type int64 int64

// float32 is the set of all IEEE-754 32-bit floating-point numbers.
type float32 float32

// float64 is the set of all IEEE-754 64-bit floating-point numbers.
type float64 float64

// complex64 is the set of all complex numbers with float32 real and
// imaginary parts.
type complex64 complex64

// complex128 is the set of all complex numbers with float64 real and
// imaginary parts.
type complex128 complex128

// string is the set of all strings of 8-bit bytes, conventionally but not
// necessarily representing UTF-8-encoded text. A string may be empty, but
// not nil. Values of string type are immutable.
type string string

// int is a signed integer type that is at least 32 bits in size. It is a
// distinct type, however, and not an alias for, say, int32.
type int int

// uint is an unsigned integer type that is at least 32 bits in size. It is a
// distinct type, however, and not an alias for, say, uint32.
type uint uint

// uintptr is an integer type that is large enough to hold the bit pattern of
// any pointer.
type uintptr uintptr

// byte is an alias for uint8 and is equivalent to uint8 in all ways. It is
// used, by convention, to distinguish byte values from 8-bit unsigned
// integer values.
type byte = uint8

// rune is an alias for int32 and is equivalent to int32 in all ways. It is
// used, by convention, to distinguish character values from integer values.
type rune = int32

// iota is a predeclared identifier representing the untyped integer ordinal
// number of the current const specification in a (usually parenthesized)
// const declaration. It is zero-indexed.
const iota = 0 // Untyped int.

// nil is a predeclared identifier representing the zero value for a
// pointer, channel, func, interface, map, or slice type.
var nil Type // Type must be a pointer, channel, func, interface, map, or slice type

// Type is here for the purposes of documentation only. It is a stand-in
// for any Go type, but represents the same type for any given function
// invocation.
type Type int

// Type1 is here for the purposes of documentation only. It is a stand-in
// for any Go type, but represents the same type for any given function
// invocation.
type Type1 int

// IntegerType is here for the purposes of documentation only. It is a stand-in
// for any integer type: int, uint, int8 etc.
type IntegerType int

// FloatType is here for the purposes of documentation only. It is a stand-in
// for either float type: float32 or float64.
type FloatType float32

// ComplexType is here for the purposes of documentation only. It is a
// stand-in for either complex type: complex64 or complex128.
type ComplexType complex64

// The append built-in function appends elements to the end of a slice. If
// it has sufficient capacity, the destination is resliced to accommodate the
// new elements. If it does not, a new underlying array will be allocated.
// Append returns the updated slice. It is therefore necessary to store the
// result of append, often in the variable holding the slice itself:
//  slice = append(slice, elem1, elem2)
//  slice = append(slice, anotherSlice...)
// As a special case, it is legal to append a string to a byte slice, like this:
//  slice = append([]byte("hello "), "world"...)
func append(slice []Type, elems ...Type) []Type

// The copy built-in function copies elements from a source slice into a
// destination slice. (As a special case, it also will copy bytes from a
// string to a slice of bytes.) The source and destination may overlap. Copy
// returns the number of elements copied, which will be the minimum of
// len(src) and len(dst).
func copy(dst, src []Type) int

// The delete built-in function deletes the element with the specified key
// (m[key]) from the map. If m is nil or there is no such element, delete
// is a no-op.
func delete(m map[Type]Type1, key Type)

// The len built-in function returns the length of v, according to its type:
//  Array: the number of elements in v.
//  Pointer to array: the number of elements in *v (even if v is nil).
//  Slice, or map: the number of elements in v; if v is nil, len(v) is zero.
//  String: the number of bytes in v.
//  Channel: the number of elements queued (unread) in the channel buffer;
//           if v is nil, len(v) is zero.
// For some arguments, such as a string literal or a simple array expression, the
// result can be a constant. See the Go language specification's "Length and
// capacity" section for details.
func len(v Type) int

// The cap built-in function returns the capacity of v, according to its type:
//  Array: the number of elements in v (same as len(v)).
//  Pointer to array: the number of elements in *v (same as len(v)).
//  Slice: the maximum length the slice can reach when resliced;
//  if v is nil, cap(v) is zero.
//  Channel: the channel buffer capacity, in units of elements;
//  if v is nil, cap(v) is zero.
// For some arguments, such as a simple array expression, the result can be a
// constant. See the Go language specification's "Length and capacity" section for
// details.
func cap(v Type) int

// The make built-in function allocates and initializes an object of type
// slice, map, or chan (only). Like new, the first argument is a type, not a
// value. Unlike new, make's return type is the same as the type of its
// argument, not a pointer to it. The specification of the result depends on
// the type:
//  Slice: The size specifies the length. The capacity of the slice is
//  equal to its length. A second integer argument may be provided to
//  specify a different capacity; it must be no smaller than the
//  length. For example, make([]int, 0, 10) allocates an underlying array
//  of size 10 and returns a slice of length 0 and capacity 10 that is
//  backed by this underlying array.
//  Map: An empty map is allocated with enough space to hold the
//  specified number of elements. The size may be omitted, in which case
//  a small starting size is allocated.
//  Channel: The channel's buffer is initialized with the specified
//  buffer capacity. If zero, or the size is omitted, the channel is
//  unbuffered.
func make(t Type, size ...IntegerType) Type

// The new built-in function allocates memory. The first argument is a type,
// not a value, and the value returned is a pointer to a newly
// allocated zero value of that type.
func new(Type) *Type

// The complex built-in function constructs a complex value from two
// floating-point values. The real and imaginary parts must be of the same
// size, either float32 or float64 (or assignable to them), and the return
// value will be the corresponding complex type (complex64 for float32,
// complex128 for float64).
func complex(r, i FloatType) ComplexType

// The real built-in function returns the real part of the complex number c.
// The return value will be floating point type corresponding to the type of c.
func real(c ComplexType) FloatType

// The imag built-in function returns the imaginary part of the complex
// number c. The return value will be floating point type corresponding to
// the type of c.
func imag(c ComplexType) FloatType

// The close built-in function closes a channel, which must be either
// bidirectional or send-only. It should be executed only by the sender,
// never the receiver, and has the effect of shutting down the channel after
// the last sent value is received. After the last value has been received
// from a closed channel c, any receive from c will succeed without
// blocking, returning the zero value for the channel element. The form
//  x, ok := <-c
// will also set ok to false for a closed channel.
func close(c chan<- Type)

// The panic built-in function stops normal execution of the current
// goroutine. When a function F calls panic, normal execution of F stops
// immediately. Any functions whose execution was deferred by F are run in
// the usual way, and then F returns to its caller. To the caller G, the
// invocation of F then behaves like a call to panic, terminating G's
// execution and running any deferred functions. This continues until all
// functions in the executing goroutine have stopped, in reverse order. At
// that point, the program is terminated with a non-zero exit code. This
// termination sequence is called panicking and can be controlled by the
// built-in function recover.
func panic(v interface{})

// The recover built-in function allows a program to manage behavior of a
// panicking goroutine. Executing a call to recover inside a deferred
// function (but not any function called by it) stops the panicking sequence
// by restoring normal execution and retrieves the error value passed to the
// call of panic. If recover is called outside the deferred function it will
// not stop a panicking sequence. In this case, or when the goroutine is not
// panicking, or if the argument supplied to panic was nil, recover returns
// nil. Thus the return value from recover reports whether the goroutine is
// panicking.
func recover() interface{}

// The print built-in function formats its arguments in an
// implementation-specific way and writes the result to standard error.
// Print is useful for bootstrapping and debugging; it is not guaranteed
// to stay in the language.
func print(args ...Type)

// The println built-in function formats its arguments in an
// implementation-specific way and writes the result to standard error.
// Spaces are always added between arguments and a newline is appended.
// Println is useful for bootstrapping and debugging; it is not guaranteed
// to stay in the language.
func println(args ...Type)

// The error built-in interface type is the conventional interface for
// representing an error condition, with the nil value representing no error.
type error interface {
    Error() string
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容