Explore the difference between methods and functions in Go language

WBOY
Release: 2024-03-26 12:21:04
Original
387 people have browsed it

Explore the difference between methods and functions in Go language

Go language is a popular statically typed programming language, which is characterized by simplicity, efficiency and strong concurrency. In Go language, method and function are two important concepts. This article will explore the differences between methods and functions in the Go language and illustrate them with specific code examples.

Methods and functions can be used to perform certain operations or logic in the Go language, but there are some differences in their definition and use. First of all, methods are functions defined on a type, which can access data of that type and are called part of the type; functions are independent and not bound to any type.

Below we use a simple example to illustrate the difference between methods and functions. First define a structure Person, which contains two fields: name and age:

package main

import "fmt"

type Person struct {
    name string
    age  int
}

func main() {
    p := Person{name: "Alice", age: 25}
    p.printName()
}

func (p Person) printName() {
    fmt.Println("Name:", p.name)
}
Copy after login

In the above code, we define A structure Person, and then a method printName is defined on the structure. An object p of type Person is created in the main function, and then the printName method of the object is called. What needs to be noted here is that when defining the method, a receiver p Person is added in front of the method name, indicating that the method belongs to the Person type.

Next let’s look at an example of a function, which is also an example of output name:

package main

import "fmt"

type Person struct {
    name string
    age  int
}

func main() {
    p := Person{name: "Bob", age: 30}
    printName(p)
}

func printName(p Person) {
    fmt.Println("Name:", p.name)
}
Copy after login

In the above code, we define a function printName, which Accepts a parameter of type Person and outputs its name. In the main function, an object of type Person is created p, and then the function printName is called and p is passed in as parameters.

As can be seen from the above two examples, methods are functions defined on types, and functions are independent. Methods depend on the type and can directly access the data of the type; functions have no such dependency. This is an important difference between methods and functions.

Another important difference is that methods can have multiple receivers, while functions can only have one. Let's look at an example:

package main

import "fmt"

type Rectangle struct {
    width, height float64
}

func (r Rectangle) area() float64 {
    return r.width * r.height
}

func (r Rectangle) perimeter() float64 {
    return 2 * (r.width + r.height)
}

func main() {
    r := Rectangle{width: 10, height: 5}
    fmt.Println("Area:", r.area())
    fmt.Println("Perimeter:", r.perimeter())
}
Copy after login

In the above code, we define a Rectangle structure, and then define area and area on the structure. perimeterTwo methods. Both methods take the Rectangle type as the receiver, that is, they can be called on objects of type

Rectangle

. This is an example of how a method can have multiple receivers.

###To summarize, the difference between methods and functions in Go language is that: methods are functions defined on types and can directly access type data; functions are independent and have no type dependencies; methods can have multiple Receiver, and function can only have one. Through the above code examples, I hope readers will have a deeper understanding of the difference between methods and functions in the Go language. ###

The above is the detailed content of Explore the difference between methods and functions in Go language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!