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 Go of Go网站的练习”,这个标题揭示了我们即将踏上一段深入理解Go语言的旅程。Go,也被称为Golang,是由Google开发的一种静态类型的、编译型的、并发型且具有垃圾回收功能的编程语言。它的设计目标是...
2. **短语**:be full of, departure lounge, because of, take the boat, go sightseeing, by coach, go for a long walk, plenty of, in front of, at the start of, as soon as, look out of, get off等。...
标准ML是具有正式规范的功能性编程语言。 它具有静态类型以防止各种各样的常见错误,而且还具有强大的类型推断功能,几乎不需要类型声明。 由于原因,很容易定义新的数据类型和结构,并且由于其强大的模块系统和 ,...
6. **方位表达** - 学习用 "in the south of", "in the north of", "in the west of", 和 "in the east of" 描述地理位置,例如:“A is in the south of B”表明A位于B的范围内,如果两者相邻则用 "on the south of...
此外,重点句型包括"I hope to go on a nature tour."(我希望去一次自然之旅)以及"I don’t want to go anywhere cold."(我不想去任何寒冷的地方)。这些知识不仅涵盖了基本的词汇运用,还涉及了不同类型的旅行...
8. **The person has a lot of money to spend on the vacation.** 描述了有足够的预算进行奢侈的旅行。 9. **I hope you can provide me with some information about the kinds of vacations that your firm can ...
如“度假”(vacation)、“野营”(camping)、“拜访”(visit)、“...旅行”(go on a sightseeing tour)、“与某人交谈”(talk to sb)、“去散步”(go for a walk)、“去钓鱼”(go fishing)以及“租录像...
9. `go on a trip`:去旅行。 10. `for example`:例如,用于举例。 11. `in a hurry`:匆忙地。 12. `surf the Internet`:上网冲浪。 这些知识点是七年级学生需要掌握的基础英语表达,涵盖了日常生活、学习、旅游...
1. 旅行相关词汇:如“到国外旅游”(travel abroad)、“旅行社”(travel agency)和“去旅行”(go traveling),以及“旅行”(tour)、“游客”(tourist)和“旅游业”(tourism)等,帮助学生掌握与旅行相关的表达方式。...
Grammar, Reading and speaking)、旅行计划(如旅行伙伴的选择,如Her friends, Her parents, A tour group,目的地特色,如Its beaches, Its food, Its volcanoes)以及奇异经历的讨论(如狗能送信,Jason’s dog...
Let's go for a bike trip." 鼓励学生享受户外活动,如骑自行车旅行,同时锻炼语言运用能力。 4. **城市印象和评价**: - "It's beautiful. The food is delicious and the people are friendly." 学生需要学会...
5. **ride, trip, travel, journey, flight, voyage, tour**:这些都是关于旅行的不同表达。ride指的是骑车或骑马;trip通常指较短的旅行;travel指广泛的旅行,可能涉及世界各地;journey侧重于陆地上的长途旅行;...
可以搭配不同的介词表达不同情境,如“go on a long train journey”指长途火车旅行。此外,它与“trip”, “tour”, “voyage”和“travel”等词在某些语境下可以互换使用。 在课程中的例子展示了这些词汇的实际...
三、英语语言点 * I love travelling in the country, but I don't like losing my way.:我喜欢乡间旅游,但我不喜欢迷路。 * love/like/enjoy doing sth.:喜欢做某事 * country:乡间、农村 * city:城市 * lose...
1. “journey”通常指长距离的陆路旅行,如“Go on a tour to Australia”。 2. “travel”作为动词,意味着进行旅行,如“travel around the world”表示周游世界,而“travel to+地点”表示到某地旅行。 3. “trip...
- **tour**:巡回旅行。指按计划访问多个地方的旅行。 - **prefer A to B**:更喜欢A而不是B。用来表达偏好选择。 在文档中还存在一些错误和遗漏,比如“atdusk”应该是“at dusk”;“thatis”应该是“that is”;...
- `a professional tour guide`:一个专业的导游 - `famous tourist attractions/places of interest`:著名景点 - `customer satisfaction`:客户满意度 - `faulty items/products`:故障产品 - `get ...
在英语中,当被问及对某个旅游的看法时,我们可以使用“What do you think of this tour?”或“How do you like this tour?”等问题,而回答可以是“It’s great!”或“It’s wonderful!”等。 第 3 题 – Do you ...
4. 他们等了三个小时去埃菲尔铁塔的顶部:They waited for three hours to go to the top of the Eiffel Tower. 在词汇应用部分,给出了几个填空题: 1. Stop working. Let's relax for one hour. (让我们休息一...