Home  >  Article  >  Backend Development  >  golang methods and functions

golang methods and functions

WBOY
WBOYOriginal
2023-05-14 14:01:08482browse

Golang is a modern programming language with many exquisite designs, including the concepts of methods and functions. Methods and functions are basic concepts that Golang programmers must be familiar with. This article will elaborate on their similarities and differences.

1. Function

Function is one of the basic components of Golang program. It consists of function name, parameter list, return value type and function body. The syntax is as follows:

func 函数名(参数列表)(返回值类型){
     函数体
}

The following is a simple example:

func sum(a int, b int) int {
     return a + b
}

This function is called sum. It requires two parameters a and b of type int, and the return value type is int. The function body includes a return statement to return the sum of a and b.

The function is called as follows:

c := sum(1, 2) //c的值为3

2. Methods

Methods are functions associated with a specific type. In Golang, the approach is to put type-specific function definitions on your own type so that these functions are called on all instances of that type.

Methods are defined like functions, but have an additional receiver parameter. It tells the method on which type of value to call. The general syntax of the method is as follows:

func (接收器变量 接收器类型) 方法名(参数列表)(返回值类型){
     函数体
}

The following is a simple example:

type Person struct {
     Name string
     Age  int
}

func (p Person) SayHello() string {
     return "Hello, my name is " + p.Name
}

func main() {
     p := Person{Name: "Tom", Age: 18}
     fmt.Println(p.SayHello()) //输出结果为 "Hello, my name is Tom"
}

In this example, we define a structure of type Person and define a SayHello method. The method sets the receiver type to the Person type, indicating that the method is called on all instances.

We create a Person instance named p and then call its SayHello method. It will return a string representing the name of the instance.

3. The difference between functions and methods

  1. A function is a piece of code that can be called anywhere, while a method is code associated with a specific type.
  2. Methods must be called through the object they belong to, while functions can be called directly.
  3. Methods can access the object's private data, but functions cannot.
  4. Methods have receiver parameters, while functions do not.
  5. Methods can be overridden, but functions cannot.

4. Summary

Golang methods and functions are the basic components of building Golang programs. A function is an independent, reusable block of code that can be called by anyone, anywhere. A method, on the other hand, is a block of code associated with a specific type and can only be called through an object of that type.

By learning methods and functions, we can better program and develop. If you want to learn more about Golang in detail, please read the relevant Golang documentation and tutorials.

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

Statement:
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
Previous article:Golang writes EthereumNext article:Golang writes Ethereum