首页 文章详情

Go语言入门(4) - 地图 Get started with the Go language

Java研发军团 | 43 2022-11-22 11:22 0 0 0
UniSMS (合一短信)

今日单词  Words for today

lack 缺少

struct 结构

sequence 结构

method 方法

declaration 声明

identifier 识别符

associate 联系

uppercase 大写字母

lowercase 小写字母

addressable 可寻趾的

Structs and methods

结构与方法

The Go language lacks classes but has a struct, which is a sequence of named elements, which are called fields. Each field has a name and a type. A method is a function with a receiver. A method declaration binds an identifier (the method name) to a method and associates the method with the receiver's base type.

GO语言没有类但是有结构,它是一个命名连续的序列,它被称作fields。每一个field都有一个名字和类型。方法是一个带有接收器的函数。声明一个识别符(方法名)绑定到方法,并将方法与接收者的基本类型相关联。

In this example, we declare a Vertex struct to contain two floating point fields, X and Y, and a method, Abs. Fields that begin with uppercase letters are public; fields that begin with lowercase letters are private. Fields and methods are addressable through the dot notation (.) and ampersands (&) signify pointers, as in C. This program prints 5.

在此示例中,我们声明 一个Vertex struct包含两个浮点fields X 和 Y 以及一个方法Abs以大写字母开头的字段是公开的;以小写字母开头的字段是私有的。字段和方法可以通过点符号 ( .)进行寻址,和号 ( &) 表示指针,就像在 C 中一样。这个程序打印5.


package main

import (
"fmt"
"math"
)

type
Vertex struct {
X
, Y float64
}

func
(v *Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func main
() {
v
:= &Vertex{3, 4}
fmt
.Println(v.Abs())
}


good-icon 0
favorite-icon 0
收藏
回复数量: 0
    暂无评论~~
    Ctrl+Enter