Home > Backend Development > Golang > golang method declaration

golang method declaration

PHPz
Release: 2023-05-16 16:54:08
Original
527 people have browsed it

Golang is a very popular programming language developed by Google. Its unique syntax and powerful performance make it widely popular. In Golang, method declaration is a very important basic knowledge as it plays an important role in object-oriented programming.

In Golang, we can use methods to extend the functionality of types. Methods can be defined in a structure or in the source code file of any type (even a basic type). In this article, we will explore the basics of Golang method declarations, including method syntax, parameter and return value types, etc.

Method syntax

In Golang, we can use the following syntax to declare methods:

func (t Type) methodName(parameterList) (returnTypeList) {

// 方法体
Copy after login

}

Among them, func is the keyword used to declare the method, Type is the type of the receiver parameter, methodName is the name of the method, parameterList is the parameter list of the method, and returnTypeList is the return of the method. List of values.

In the above syntax, Type can be a structure type or other types. The receiver parameter is the type on which the method is defined, which means we can access the method using the instance on which the method is called. If we define a struct type, we can use it to call methods like this:

type Rectangle struct {

width, height float64
Copy after login

}

func (r Rectangle) area () float64 {

return r.width * r.height
Copy after login

}

r := Rectangle{3, 4}
fmt.Println(r.area())

In the above code , we define a Rectangle type and an area() method. The receiver parameter of the area() method is of Rectangle type. We create a Rectangle instance r and use it to call the area() method to calculate the area of ​​the rectangle.

Then take a look at the parameter list and return value list. The parameter list contains zero or more parameters, and the type of these parameters can be any valid Golang type. The return value list represents the return value of the method, which consists of 0 or more return values. The type of these return values ​​can also be any valid Golang type.

Here is an example that demonstrates how to define a method with two parameters and a return value:

type Person struct {

name string
age  int
Copy after login

}

func (p Person) fullName(firstName string, lastName string) string {

p.name = firstName + " " + lastName
return p.name
Copy after login

}

person := Person{"Jerry", 28}
fmt.Println( person.fullName("John", "Doe"))

In this example, we define a Person type and a fullName() method. The fullName() method has two parameters: firstName and lastName. It also has a return value of type string. In the fullName() method, we combine firstName and lastName into a complete name, then assign it to the name field in the Person structure, and return this name.

Parameter and return value types

The parameter list and return value list of Golang methods are very flexible, they can be any valid Golang data type. In a method definition, the parameters and return value types can be the same or different. Let's take a look at the following example:

type Student struct {

name      string
age       int
grade     int
addresses []string
Copy after login

}

func (s *Student) changeName(name string) {

s.name = name
Copy after login

}

func (s *Student) getAddresses() []string {

return s.addresses
Copy after login

}

student := Student{"Joe", 12, 6, [] string{"Beijing", "Shanghai"}}
fmt.Println(student.name)
student.changeName("Tom")
fmt.Println(student.name)

addresses := student.getAddresses()
fmt.Println(addresses)

In the above code, we define a Student structure and two methods: changeName() and getAddresses(). The receiver parameter of the changeName() method is a pointer to the Student structure, which has no return value, while the receiver parameter of the getAddresses() method is a pointer to the Student structure, and its return value is a string array.

In the changeName() method, we set the name field of the Student instance to a new value, and in the getAddresses() method, we directly return the addresses field of the Student instance.

Summary

As mentioned in this article, method declaration is a very important part of Golang programming. Among them, the method syntax, parameters and return value types are the three aspects we need to pay attention to. By understanding the definition and use of Golang methods, we can better utilize Golang's excellent features and develop more efficient and excellent applications.

The above is the detailed content of golang method declaration. For more information, please follow other related articles on the PHP Chinese website!

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