`

Go语言旅行三 A Tour of Go

    博客分类:
  • Go
go 
阅读更多
Basic types
基本类型

bool

string

int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
     // represents a Unicode code point

float32 float64

complex64 complex128
package main

import (
	"math/cmplx"
	"fmt"
)

var (
	ToBe bool = false
	MaxInt uint64 = 1<<64 - 1
	z complex128 = cmplx.Sqrt(-5+12i)
)

func main() {
	const f = "%T(%v)\n"
	fmt.Printf(f, ToBe, ToBe)
	fmt.Printf(f, MaxInt, MaxInt)
	fmt.Printf(f, z, z)
}

输出:
bool(false)
uint64(18446744073709551615)
complex128((2+3i))

%T 是类型Type(%v)是值Value

Structs
结构体
A struct is a collection of fields.
结构体是很多字段的集合
package main

import "fmt"

type Vertex struct {
	X int
	Y int
}

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

输出:
{1 2}

Struct Fields
结构体字段
Struct fields are accessed using a dot.
结构体字段用点.访问
package main

import "fmt"

type Vertex struct {
	X int
	Y int
}

func main() {
	v := Vertex{1, 2}
	v.X = 4
	fmt.Println(v.X)
}

输出:
4

Pointers

Go has pointers, but no pointer arithmetic.
Go有指针,但是没有指针运算
Struct fields can be accessed through a struct pointer. The indirection through the pointer is transparent.
结构体字段能够通过结构体指针访问,很容易通过指针间接访问
package main

import "fmt"

type Vertex struct {
	X int
	Y int
}

func main() {
	p := Vertex{1, 2}
	q := &p
	q.X = 1e9
	fmt.Println(p)
}

输出:
{1000000000 2}

Struct Literals
结构体值
A struct literal denotes a newly allocated struct value by listing the values of its fields.
结构体值表示一个新分配结构体值通过列出它所有的字段值
You can list just a subset of fields by using the Name: syntax. (And the order of named fields is irrelevant.)
你也可以只通过名字语法勒出一部分字段值,顺序无所谓
The special prefix & constructs a pointer to a struct literal.
指定前缀&构造一个结构体字面值的指针
package main

import "fmt"

type Vertex struct {
	X, Y int
}

var (
	p = Vertex{1, 2}  // has type Vertex
	q = &Vertex{1, 2} // has type *Vertex
	r = Vertex{X: 1}  // Y:0 is implicit
	s = Vertex{}      // X:0 and Y:0
)

func main() {
	fmt.Println(p, q, r, s)
}

输出:
{1 2} &{1 2} {1 0} {0 0}

The new function
new函数
The expression new(T) allocates a zeroed T value and returns a pointer to it.
表达式 new(T)返回一个分配了0默认值的T类型的指针
var t *T = new(T)
or
t := new(T)
package main

import "fmt"

type Vertex struct {
	X, Y int
}

func main() {
	v := new(Vertex)
	fmt.Println(v)
	v.X, v.Y = 11, 9
	fmt.Println(v)
}

输出:
&{0 0}
&{11 9}

Maps

A map maps keys to values.
map匹配键和值
Maps must be created with make (not new) before use; the nil map is empty and cannot be assigned to.
Maps用之前必须用make创建,没有make的map是空的不能赋值
package main

import "fmt"

type Vertex struct {
	Lat, Long float64
}

var m map[string]Vertex

func main() {
	m = make(map[string]Vertex)
	m["Bell Labs"] = Vertex{
		40.68433, 74.39967,
	}
	fmt.Println(m["Bell Labs"])
}

输出:
{40.68433 74.39967}

Map literals are like struct literals, but the keys are required.
Map的字面值和结构体字面值一样,但是必须有键值

package main

import "fmt"

type Vertex struct {
	Lat, Long float64
}

var m = map[string]Vertex{
	"Bell Labs": Vertex{
		40.68433, -74.39967,
	},
	"Google": Vertex{
		37.42202, -122.08408,
	},
}

func main() {
	fmt.Println(m)
}

输出:
map[Google:{37.42202 -122.08408} Bell Labs:{40.68433 -74.39967}]

If the top-level type is just a type name, you can omit it from the elements of the literal.
如果顶级类型只是类型名的话,可以省略
package main

import "fmt"

type Vertex struct {
	Lat, Long float64
}

var m = map[string]Vertex{
	"Bell Labs": {40.68433, -74.39967},
	"Google":    {37.42202, -122.08408},
}

func main() {
	fmt.Println(m)
}

输出:
map[Google:{37.42202 -122.08408} Bell Labs:{40.68433 -74.39967}]

Mutating Maps
Maps变型

Insert or update an element in map m:
插入或修改map m的一个元素
m[key] = elem
Retrieve an element:
提取一个元素
elem = m[key]
Delete an element:
删除一个元素
delete(m, key)
Test that a key is present with a two-value assignment:
测试键里是否有值
elem, ok = m[key]
If key is in m, ok is true. If not, ok is false and elem is the zero value for the map's element type.
如果键有对应值,ok 是true,如果没有,ok是false,并且对应map's元素类型elem是0值
Similarly, when reading from a map if the key is not present the result is the zero value for the map's element type.
相似的是当从map取值的时候如果键值不存在,返回结果也是map's元素类型默认值

package main

import "fmt"

func main() {
	m := make(map[string]int)

	m["Answer"] = 42
	fmt.Println("The value:", m["Answer"])

	m["Answer"] = 48
	fmt.Println("The value:", m["Answer"])

	delete(m, "Answer")
	fmt.Println("The value:", m["Answer"])

	v, ok := m["Answer"]
	fmt.Println("The value:", v, "Present?", ok)
}

输出:
The value: 42
The value: 48
The value: 0
The value: 0 Present? false
分享到:
评论

相关推荐

    A Tour of C++, 2nd Edition

    A Tour of C++ (2nd Edition) (C++ In-Depth Series) By 作者: Bjarne Stroustrup ISBN-10 书号: 0134997832 ISBN-13 书号: 9780134997834 Edition 版本: 2 出版日期: 2018-07-09 pages 页数: 815 In A Tour of ...

    C++语言导学.A Tour of C++.pdf

    《计算机科学丛书:C++语言导学》作者是C++语言的设计者和最初实现者,写作本书的目的是让有经验的程序员快速了解C++现代语言。书中几乎介绍了C++语言的全部核心功能和重要的标准库组件,以很短的篇幅将C++语言的...

    C++语言导学 A Tour of C++(C++之父写的入门书)

    《计算机科学丛书:C++语言导学》作者是C++语言的设计者和最初实现者,写作本书的目的是让有经验的程序员快速了解C++现代语言。书中几乎介绍了C++语言的全部核心功能和重要的标准库组件,以很短的篇幅将C++语言的...

    手工安装GO官方教程A Tour of Go.docx

    A Tour of Go是官方提供的一个关于go语言短小精悍的教程,覆盖了包、变量、函数、数据类型等基础知识,也包括了方法、接口、并发编程等高级内容,读完并通过其中的练习题,短时间即可入门GO了。 该教程是一个WEB服务...

    A tour of C++ 2nd

    In A Tour of C++, Second Edition, Bjarne Stroustrup, the creator of C++, describes what constitutes modern C++. This concise, self-contained guide covers most major language features and the major ...

    A Tour of C++ 2nd

    A Tour of C++ 2nd ,c++学习资料

    A Tour of C++

    A Tour of C++ 一本不错的学习C++的书

    [mirror] A Tour of Go.zip

    [mirror] A Tour of Go

    A Tour of TensorFlow

    Abstract—Deep learning is a branch of artificial intelligence employing deep neural network architectures that has significantly advanced the state-of-the-art in computer vision, speech recognition, ...

    A Tour of C++/ C++语言导学

    高清非扫描版,带书签。Bjarne Stroustrup。。。。。。

    Bjarne Stroustrup A Tour of C++ (2013).pdf

    Bjarne Stroustrup A Tour of C++

    Whirlwind Tour Of Python

    如果你懂一些编程知识,比如学过C,Java等语言开发工具,或者你是一位具有工程背景的研究者或者工作者,那么这本书将会快速带你走进python世界,这是初学python者最佳学习资料,本附件是书中的练习题等源代码。...

    A wavelet tour of signal processing 3rd edition

    英文三版。 Mallet 的小波分析经典教程。全书13章800多页,非常详尽的介绍了小波分析原理和应用。很经典的教程。

    A Guided Tour Of Mathematical Physics (By Roel Snieder, Department Of Geophysics, Utrecht Univers.pdf

    A Guided Tour Of Mathematical Physics By Roel Snieder

    A Whirlwind Tour of Python

    对于想快速了解和使用python的人,这是一本非常好的入门书,与《python数据科学手册》为姊妹篇。

    A Wavelet Tour of Signal Processing 3rd Edition

    Mallat的《信号处理的小波导引》的第三版 经典程度不用多说 PDF版本

    新版图书 A.Tour.of.C++.2nd.Edition.2018.7

    Stroustrup presents the C++ features in the context ... The tour covers some extensions being made for C++20, such as concepts and modules, and ends with a discussion of the design and evolution of C++.

Global site tag (gtag.js) - Google Analytics