Go function syntax explanation

huangapple go评论116阅读模式
英文:

Go function syntax explanation

问题

(ip IP) 是一个函数的参数声明。在这个上下文中,它表示函数的参数名为 ip,类型为 IP

英文:

I am reading one book.

One function came out:

> func (ip IP) DefaultMask() IPMask

Source code for this function is located inside the net package:

  1. func (ip IP) DefaultMask() IPMask {
  2. if ip = ip.To4(); ip == nil {
  3. return nil
  4. }
  5. switch true {
  6. case ip[0] < 0x80:
  7. return classAMask
  8. case ip[0] < 0xC0:
  9. return classBMask
  10. default:
  11. return classCMask
  12. }
  13. }

Question is: what is (ip IP)?

答案1

得分: 1

“方法声明” - 定义接收器基本类型的方法的方式。

https://golang.org/ref/spec#Function_declarations

“Method declarations” - 定义接收器基本类型的方法的方式。

英文:

https://golang.org/ref/spec#Function_declarations

"Method declarations" - the way to define a method for receiver base type.

答案2

得分: 0

这个语法允许你从一个IP类型中调用DefaultMask(),类似于其他语言中的成员函数的工作方式:

  1. ip.DefaultMask()

其中的 (ip IP) 基本上表示了 "thiscall" 参数。

英文:

This syntax allows you to call DefaultMask() from an IP type, similar to how member functions work in other languages:

  1. ip.DefaultMask()

The (ip IP) basically represents the "thiscall" argument.

huangapple
  • 本文由 发表于 2015年2月9日 11:21:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/28402309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定