Let's talk about why it is not recommended to use this/self for the receiver name in go.

藏色散人
Release: 2023-03-31 15:01:15
forward
1736 people have browsed it

This article brings you relevant knowledge about golang. It mainly introduces why it is not recommended to use this/self for the receiver name in go. What is the reason? If you are interested, let’s take a look. I hope everyone has to help.

Let's talk about why it is not recommended to use this/self for the receiver name in go.

Why is it not recommended to use this or self for receiver name in go?

Lets talk about why it is not recommended to use this/self for the receiver name in go.

In Go, receiver refers to the receiver as a method parameter, which can be a value type or a pointer type. When defining a receiver, it is not recommended to use this or self as the name of the receiver.

In other languages, such as Java and C, this or self are used to represent references to the current instance or object, but in Go, this and self are invalid identifiers and cannot be used for variable names or methods. name.

Although you can use this or self as a variable name or method name in Go, this does not conform to the style and convention of Go code. According to the Go code specification, it is recommended to use the first letter of the receiver type as the receiver name, such as:

type myType struct{}// value receiverfunc (m myType) myMethod() {
    // ...}// pointer receiverfunc (m *myType) myMethod2() {
    // ...}
Copy after login

Recommended learning: "go Video Tutorial"                                              

The above is the detailed content of Let's talk about why it is not recommended to use this/self for the receiver name in go.. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
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