Home > Backend Development > Golang > golang method assignment

golang method assignment

WBOY
Release: 2023-05-21 20:39:37
Original
786 people have browsed it

Go language is a language that supports object-oriented programming. In object-oriented programming, an object contains properties and methods. When we define a structure, we can not only define attribute types, but also assign methods to the structure to give the structure more functions. This article will introduce how to assign values ​​to structures in Go language.

Define the structure

In the Go language, we use the keyword type to define the structure. For example, we define a student structure:

type Student struct {
    Name    string
    Age     int
    Address string
    Phone   string
}
Copy after login

This structure contains the student's name, age, address and phone number.

Define method

The format of the method is as follows:

func (s Student) methodName() {
    // 方法实现
}
Copy after login

Among them, func represents the function definition, and s Student in parentheses indicates that this method belongs to the Student structure; methodName is the method name , the method name should start with a capital letter, indicating that it is a public method; the implementation content of the method is in curly brackets.

Now, we define a method display() in the student structure to display student information:

func (s Student) display() {
    fmt.Printf("Name: %s
Age: %d
Address: %s
Phone: %s
", s.Name, s.Age, s.Address, s.Phone)
}
Copy after login

In this method, we use the Printf function to output the student's name, age, and address and phone number. Note that the properties of the structure can be used directly in the method.

Assigning methods to structures

After we define the structure and methods, we can assign the methods to the structure. In Go language, you can use the type keyword and func keyword to define methods for structure types. For example, we assign the display() method to the Student structure:

type Student struct {
    Name    string
    Age     int
    Address string
    Phone   string
}

func (s Student) display() {
    fmt.Printf("Name: %s
Age: %d
Address: %s
Phone: %s
", s.Name, s.Age, s.Address, s.Phone)
}
Copy after login

The code here is the same as the previous code, except that the structure definition and method definition are merged together. Now, we create a variable s of type Student and call its display() method:

s := Student{"Tom", 18, "Beijing, China", "123456789"}
s.display()
Copy after login

The code here creates a student object named s and calls its display() method. When running this line of code, the console will output the following:

Name: Tom
Age: 18
Address: Beijing, China
Phone: 123456789
Copy after login

Structure methods can access the properties of the structure, which makes it easier for them to process the structure's data. I hope this article can give you a deeper understanding of the concept and application of Golang method assignment.

The above is the detailed content of golang method assignment. 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