golang定义一个方法_在Go中定义方法

golang定义一个方法

介绍 (Introduction)

Functions allow you to organize logic into repeatable procedures that can use different arguments each time they run. In the course of defining functions, you’ll often find that multiple functions might operate on the same piece of data each time. Go recognizes this pattern and allows you to define special functions, called methods, whose purpose is to operate on instances of some specific type, called a receiver. Adding methods to types allows you to communicate not only what the data is, but also how that data should be used.

函数使您可以将逻辑组织为可重复的过程,这些过程每次运行时都可以使用不同的参数。 在定义函数的过程中,您经常会发现多个函数可能每次都对相同的数据进行操作。 Go可以识别这种模式,并允许您定义称为方法的特殊功能,其目的是对某些特定类型的实例(称为接收器)进行操作 。 向类型添加方法不仅使您可以传达数据是什么,还可以传达如何使用数据。

定义方法 (Defining a Method)

The syntax for defining a method is similar to the syntax for defining a function. The only difference is the addition of an extra parameter after the func keyword for specifying the receiver of the method. The receiver is a declaration of the type that you wish to define the method on. The following example defines a method on a struct type:

定义方法的语法类似于定义函数的语法。 唯一的区别是在func关键字之后添加了一个额外的参数,用于指定方法的接收者。 接收器是您希望在其上定义方法的类型的声明。 以下示例在结构类型上定义了一个方法:

package main

import "fmt"

type Creature struct {
    Name     string
    Greeting string
}

func (c Creature) Greet() {
    fmt.Printf("%s says %s", c.Name, c.Greeting)
}

func main() {
    sammy := Creature{
        Name:     "Sammy",
        Greeting: "Hello!",
    }
    Creature.Greet(sammy)
}

If you run this code, the output will be:

如果运行此代码,输出将是:


   
   
   
Output
Sammy says Hello!

We created a struct called Creature with string fields for a Name and a Greeting. This Creature has a single method defined, Greet. Within the receiver declaration, we assigned the instance of Creature to the variable c so that we could refer to the fields of the Creature as we assemble the greeting message in fmt.Printf.

我们创建了一个名为Creature的结构,其中包含NameGreeting string字段。 这个Creature有一个定义的方法Greet 。 在接收者声明中,我们将Creature的实例分配给了变量c以便在我们在fmt.Printf组装问候消息时fmt.Printf Creature的字段。

In other languages, the receiver of method invocations is typically referred to by a keyword (e.g. this or self). Go considers the receiver to be a variable like any other, so you’re free to name it whatever you like. The style preferred by the community for this parameter is a lower-case version of the first character of the receiver type. In this example, we used c because the receiver type was Creature.

在其他语言中,方法调用的接收者通常由关键字(例如thisself )引用。 Go认为接收器是一个和其他变量一样的变量,因此您可以随意命名。 社区对此参数首选的样式是接收器类型的第一个字符的小写版本。 在此示例中,我们使用c因为接收器类型为Creature

Within the body of main, we created an instance of Creature and specified values for its Name and Greeting fields. We invoked the Greet method here by joining the name of the type and the name of the method with a . and supplying the instance of Creature as the first argument.

main ,我们创建了Creature的实例,并为其NameGreeting字段指定了值。 我们在此处通过将类型名称和方法名称与进行连接来调用Greet方法. 并提供Creature实例作为第一个参数。

Go provides another, more convenient, way of calling methods on instances of a struct as shown in this example:

Go提供了另一种更方便的在结构实例上调用方法的方式,如以下示例所示:

package main

import "fmt"

type Creature struct {
    Name     string
    Greeting string
}

func (c Creature) Greet() {
    fmt.Printf("%s says %s", c.Name, c.Greeting)
}

func main() {
    sammy := Creature{
        Name:     "Sammy",
        Greeting: "Hello!",
    }
    sammy.Greet()
}

If you run this, the output will be the same as the previous example:

如果运行此命令,则输出将与前面的示例相同:


   
   
   
Output
Sammy says Hello!

This example is identical to the previous one, but this time

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值