Golang中的方法用法和注意事项介绍

2021年3月29日18:05:46 发表评论 770 次浏览

语言支持方法。 Go方法与Go函数类似, 但有一个区别, 即该方法中包含一个接收器参数。借助接收方参数, 该方法可以访问接收方的属性。在此, 接收器可以是结构类型或非结构类型。在代码中创建方法时, 接收者和接收者类型必须存在于同一包中。并且不允许你在其他包中已经定义了接收器类型的方法, 其中包括int, string等内置类型。如果尝试这样做, 则编译器将给出错误。

语法如下:

func(reciver_name Type) method_name(parameter_list)(return_type){
// Code
}

在此, 可以在方法内访问接收器。

结构类型接收器的方法

Go语言中, 允许你定义其接收者为结构类型的方法。可以在方法内部访问此接收器, 如以下示例所示:

例子:

// Go program to illustrate the
// method with struct type receiver
package main
  
import "fmt"
  
// Author structure
type author struct {
     name      string
     branch    string
     particles int
     salary    int
}
  
// Method with a receiver
// of author type
func (a author) show() {
  
     fmt.Println( "Author's Name: " , a.name)
     fmt.Println( "Branch Name: " , a.branch)
     fmt.Println( "Published articles: " , a.particles)
     fmt.Println( "Salary: " , a.salary)
}
  
// Main function
func main() {
  
     // Initializing the values
     // of the author structure
     res := author{
         name:      "Sona" , branch:    "CSE" , particles: 203, salary:    34000, }
  
     // Calling the method
     res.show()
}

输出如下:

Author's Name:  Sona
Branch Name:  CSE
Published articles:  203
Salary:  34000

非结构型接收机的方法

Go语言中, 只要类型和方法定义存在于同一包中, 就可以使用非结构类型接收器创建方法。如果它们存在于int, string等不同的包中, 则编译器将给出错误, 因为它们是在不同的包中定义的。

例子:

// Go program to illustrate the method
// with non-struct type receiver
package main
  
import "fmt"
  
// Type definition
type data int
  
// Defining a method with
// non-struct type receiver
func (d1 data) multiply(d2 data) data {
     return d1 * d2
}
  
/*
// if you try to run this code, // then compiler will throw an error
func(d1 int)multiply(d2 int)int{
return d1 * d2
}
*/
  
// Main function
func main() {
     value1 := data(23)
     value2 := data(20)
     res := value1.multiply(value2)
     fmt.Println( "Final result: " , res)
}

输出如下:

Final result:  460

指针接收器的方法

在Go语言中, 你可以使用指针接收器。在指针接收器的帮助下, 如果对方法进行的更改将反映在调用者中, 这对于值接收器是不可能的。

语法如下:

func (p *Type) method_name(...Type) Type {
// Code
}

例子:

// Go program to illustrate pointer receiver
package main
  
import "fmt"
  
// Author structure
type author struct {
     name      string
     branch    string
     particles int
}
  
// Method with a receiver of author type
func (a *author) show(abranch string) {
     (*a).branch = abranch
}
  
// Main function
func main() {
  
     // Initializing the values
     // of the author structure
     res := author{
         name:   "Sona" , branch: "CSE" , }
  
     fmt.Println( "Author's name: " , res.name)
     fmt.Println( "Branch Name(Before): " , res.branch)
  
     // Creating a pointer
     p := &res
  
     // Calling the show method
     p.show( "ECE" )
     fmt.Println( "Author's name: " , res.name)
     fmt.Println( "Branch Name(After): " , res.branch)
}

输出如下:

Author's name:  Sona
Branch Name(Before):  CSE
Author's name:  Sona
Branch Name(After):  ECE

方法可以接受指针和值

众所周知, 在Go中, 当一个函数具有值参数时, 它将仅接受参数的值, 如果你尝试将指针传递给值函数, 则它将不接受, 反之亦然。但是Go方法可以接受值和指针, 无论它是使用指针还是值接收器定义的。如下例所示:

例子:

// Go program to illustrate how the
// method can accept pointer and value
  
package main
  
import "fmt"
  
// Author structure
type author struct {
     name   string
     branch string
}
  
// Method with a pointer
// receiver of author type
func (a *author) show_1(abranch string) {
     (*a).branch = abranch
}
  
// Method with a value
// receiver of author type
func (a author) show_2() {
  
     a.name = "Gourav"
     fmt.Println( "Author's name(Before) : " , a.name)
}
  
// Main function
func main() {
  
     // Initializing the values
     // of the author structure
     res := author{
         name:   "Sona" , branch: "CSE" , }
  
     fmt.Println( "Branch Name(Before): " , res.branch)
  
     // Calling the show_1 method
     // (pointer method) with value
     res.show_1( "ECE" )
     fmt.Println( "Branch Name(After): " , res.branch)
  
     // Calling the show_2 method
     // (value method) with a pointer
     (&res).show_2()
     fmt.Println( "Author's name(After): " , res.name)
}

输出如下:

Branch Name(Before):  CSE
Branch Name(After):  ECE
Author's name(Before) :  Gourav
Author's name(After):  Sona

方法和功能之间的差异

方法 函数
它包含接收器。 它不包含接收器。
它可以接受指针和值。 它不能同时接受指针和值。
可以在程序中定义相同名称但不同类型的方法。 程序中不允许定义相同名称但不同类型的函数。

木子山

发表评论

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