Function definition syntax
Consistent with most languages, the function definitions in Go are basically consistent with other languages
func function_name(Parameter-list) { // function body... } func function_name(Parameter-list) Return-Type { // function body... } func function_name(Parameter-list) (Multiple Return-Types){ // function body.. }
func: function defines keywords
function_name: function name. Go language mainly uses camel-case (camel) naming method. It also uses the first letter to distinguish the functions according to the nature of the function. The specific chapters will explain the specifications of Go language in the following special chapters.
Parameter-list: Parameter list, if not, you can ignore it
Return-Type/Multiple Return-Types: The type of return value, that is, the type of return value. Here we specifically distinguish three forms:
- No return value/single return value/multiple return value
- The other two are the minor differences in the use of brackets. A single return type generally does not add brackets, while a multi-return value requires brackets.
- In addition, for the sake of aesthetics, it is recommended to have a space between the brackets after the parameter list and the return type.
Function definition example
No return value function
func HelloFunc() { ("Hello, World") }
Single return value function
This example demonstrates the parameter definition method and return value type definition method
func HelloFunc(msg string) string { return "Hello, " + msg }
Multiple return value function
The second return value type is usually used to pass back errors, which facilitates the program to handle exceptions.
func HelloFunc(msg string) (string, error) { return "Hello, " + msg, nil }
Let's take a look at the complete code implementation. When calling HelloFunc in the main function, two variables are also required to receive the corresponding values
package main import "fmt" func HelloFunc(msg string) (string, error) { return "Hello, " + msg, nil } func main() { printString, err := HelloFunc("World") if err == nil { (printString) } }
Method definition
There are no classes in Go, so a method definition similar to function definition is provided in Go. By adding the Reciever type before the function name, a method can use the properties of Reciver. Let's take a look at the syntax:
func (Reciever-Name Type) function_name(Parameter-list) (Multiple Return-Types){ // function body.. }
Since most of the definitions are the same as the above function definitions, I will not repeat them here, and I will only introduce the newly added parts:
Receiver-Name: The type must be a custom type, not a built-in int, string, etc. If used, an error will be reported during the compilation stage.
Method example
Basic Type
Let’s look at a method that is implemented through basic types. Here we use a knowledge point that we have never learned - custom type type. We will explain it in detail later, so there is no need to worry about it here. This sentence means that variables defined again through custom type mystring are essentially the same type as string.
type mystring string
Pay attention to our function definition here. Before the function name, there is an additional definition of (msg mystring). In the function body, we can also use msg directly.
func (msg mystring) HelloFunc() { str := "Hello, " + msg (str) }
When calling in the main function, unlike the above function call, we directly use the method HelloFunc that calls mymsg, to implement a method similar to the above example
var mymsg mystring mymsg = "World" ()
The complete code is as follows
package main import "fmt" type mystring string func (msg mystring) HelloFunc() { str := "Hello, " + msg (str) } func main() { var mymsg mystring mymsg = "World" () }
Structure type
In fact, judging from the source code of each project, the method is more used with structs and interfaces. These will be explained in detail later, and you only need to understand them here. Here is a simple example, let's calculate the area of a rectangle.
- Defines a structure rect, which contains two properties: length and width
- The method of calculating area() is defined as the structure type, so that the length and width can be used to calculate the area in the body.
- In the main function, a structure is defined, and the initialization length and width are 3 and 4 respectively.
- Calling the custom structure () to complete the area calculation
package main import "fmt" type rect struct { width float64 height float64 } func (r rect) area() float64 { return * } func main() { r := rect{3, 4} rectArea := () ("Rect area is %v\n", rectArea) }
This is the article about understanding the usage of functions and methods in Go. For more related Go functions and methods, please search for my previous article or continue browsing the related articles below. I hope you can support me in the future!