Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to declare a method in Go

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to declare the method in Go. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Method

Go supports some object-oriented programming features, and the method is one of these supported features.

Method declaration

In Go, we can explicitly declare a method for types T and * T, where type T must meet four conditions:

T must be a definition type

T must be defined in the same code package as this method declaration

T cannot be a pointer type

T cannot be an interface type.

Types T and * T are called receiver type types of their respective methods. Type T is called the parent base type (receiver base type) of all methods declared by types T and * T

If we declare a method for a type, we can later say that the type has this method, and a method declaration is similar to a function declaration, but with an additional parameter declaration part than the function declaration. This additional parameter declaration section can contain only one parameter whose type is the parent type of this method, which is called the owner parameter (receiver parameter) declared by this method. This attribute master parameter declaration must be wrapped in a pair of parentheses (). This attribute master parameter declaration section must be between the func keyword and the method name

Here is an example of a method declaration:

/ / Age and int are two different types. We cannot declare methods for int and * int// types, but we can declare methods for Age and * Age types. Type Age intfunc (age Age) LargerThan (an Age) bool {return age > a} func (age * Age) Increase () {* age++} / / declares the method for the custom function type FilterFunc. Type FilterFunc func (in int) boolfunc (ff FilterFunc) Filte (in int) bool {return ff (in)} / / declares the method for the custom mapping type StringSet. Type StringSet map [string] struct {} func (ss StringSet) Has (key string) bool {_, present: = ss [key] return present} func (ss StringSet) Add (key string) {ss [key] = struct {}} func (ss StringSet) Remove (key string) {delete (ss, key)} / / declares methods for the custom structure type Book and its pointer type * Book. Type Book struct {pages int} func (b Book) Pages () int {return b.pages} func (b * Book) SetPages (pages int) {b.pages = pages} each method corresponds to an implicitly declared function

For each method declaration, the compiler automatically implicitly declares a corresponding function. For example, for the two methods declared for types Book and * Book in the example in the previous section, the compiler automatically declares the following two functions:

Func Book.Pages (b Book) int {return b.pages / / this function body is the same as Pages method body of Book type} func (* Book) .SetPages (b * Book, pages int) {b.pages = pages / / this function body is the same as SetPages method body of * Book type}

In the two implicit function declarations above, the owner parameter declarations of their respective method declarations are inserted in the first place of the normal parameter declaration. Their function bodies are the same as the method bodies of their respective explicit methods.

The two implicit function names Book.Pages and (* Book) .SetPages are in the form of aType.MethodName. We cannot explicitly declare a function whose name is in this form because this form is not a legal identifier. Such functions can only be implicitly declared by the compiler. But we can call these implicitly declared functions in the code

Method prototype (method prototype) and method set (method set)

A method prototype can be thought of as a function prototype without the func keyword. We can think of each method declaration as consisting of a func keyword, a master parameter declaration part, a method prototype, and a method body.

Method values and method calls

Method is actually a special function. Methods are also often referred to as member functions. When a type has a method, each value of that type will have an immutable member of the function type (a field similar to a structure). The name of this member is the method name, and its type is the same as the type declared by the function that does not include the owner part in the declaration of this method. The member function of a value can also be called the method of this value.

A method call is actually a member function that calls a value. Assuming that a value v has a method named m, this method can be represented by the selector syntax form v. m.

The following example shows how to call the methods declared for the Book and * Book types:

Package mainimport "fmt" type Book struct {pages int} func (b Book) Pages () int {return b.pages} func (b * Book) SetPages (pages int) {b.pages = pages} func main () {var book Book fmt.Printf ("% T\ n", book.Pages) / / func () int fmt.Printf ("% T\ n", (& book) .SetPages) / / func (int) / / & the book value has an implicit method Pages. Fmt.Printf ("% T\ n", (& book) .pages) / / func () int / / calls these three methods. (& book) .SetPages book.SetPages (123) / / equivalent to the previous line fmt.Println (book.Pages ()) / / 123 fmt.Println ((& book) .SetPages ()) / / 123}

Like the ordinary parameter transfer, the parameter transfer of the main parameter is also a process of value replication. Therefore, the modification of the direct part of the main parameters in the method will not be reflected in the outside of the method.

This is the end of this article on "how to declare methods in Go". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report