Why do WE need a function
Functions are called functions in all programming languages, Java, PHP, Python, JS, etc., all called functions.
Function function
A function can encapsulate the repetitive, or function-specific, into something that is easy to call.
Note: In Go, functions support closures.
When no function is used
Package main import "FMT" func main() {// Simulate an open file, write a line into the file, Println(FMT.Sprintf(" open %s file ", Sprintf(" write %s to %s file ", file_name, w_content)) FMT.Println(FMT.Sprintf(" write %s to %s file ", file_name, w_content) File_name)) // If you write to another file, Println(" %s ", "%s", "%s"); Sprintf(" write %s to %s ", file_name2, w_content2)) FMT.Println(FMT.Sprintf(" write %s to %s ", file_name2, w_content2)) FMT.Println(FMT. file_name2))}Copy the code
After using the function
Encapsulate the same functionality as a function.
Package main import "FMT" func w_file(filename string, w_content string) {FMT.Println(FMT.Sprintf(" open %s file ", Sprintf(" write %s to %s file ", filename, w_content)) FMT.Println(FMT.Sprintf(" write %s to %s file ", filename, w_content) Filename)} func main() {// wrap the same function as w_file("a.txt", "love my China ") w_file("b.txt"," Chinese power ")}Copy the code
**ps:** But it is obvious that using functions to extract the same functionality makes the code simpler and cleaner.
Function USES
Naming rules for function names
Function names should be humped, such as getName and connectData.
grammar
In Go, the func keyword is used to define the functional language.
Func function name ([parameter 1 Parameter type 1, parameter 2 Parameter type 2,... ) [(return value Return value type,...)] {logical code} // Parentheses indicate optional argumentsCopy the code
No parameter, no return value
Package main import "FMT" func say1() {FMT.Println(" )}Copy the code
Argument, no return value
Func say2(c string) {FMT.Println(" I can finally say "+ c + ")}Copy the code
With or without arguments, return values
Func say3(c string) (string) {FMT.Println(" + c + ") return "oh yeah"}Copy the code
The main function
Func main() {say1() say2(" hello ") result := say3(" hello ") FMT.Printf(result)}Copy the code
Call a function
Function name + parentheses to call the function, if any arguments are passed in.
Package main import "FMT" func say() string{FMT.Println(" I can finally speak... ) return ""} func main() {// function name + parentheses call function say() // result: I can finally speak... }Copy the code
** Note :** may not be accepted if the function returns a value.
Function parameter characteristic
In Go, you can write this if the function arguments are of the same type.
Func say(arg1, arg2, arg3, arg4 string) {FMT.Println(" I can finally speak... Func say(arg1, arg2, int, arg3, arg4 string) {//arg1, arg2, arg3, The arg4 argument is of type string fmt.Println(" I can finally speak..."). )}Copy the code
If the parameter does not contain a type, the type encountered later will prevail.
Function of the… parameter
. Arguments, also called variable-length arguments, are a bit like *args in Python.
If you do not know how many parameters to receive, the number of parameters received will be placed in… In the.
. Parameters need to be at the end.
code
Package main import "FMT" func say(name string, content... String) {ftt.println (content) // Result :[] ftt.printf ("%T\n", content) // Result :[]string, Println(" I am "+name, "I said :") // loop slice for _, V := range content {FMT.Println(v)}} func main() {say(" FMT ", "666", "ok", "oh yeah "); }Copy the code
Note: The parameters are… Type, its value is a slice type.
The return value of the function
The return value is one
Package main import "FMT" // Returns a func say1() string {return "OK"}Copy the code
The return value is multiple and needs to be enclosed in parentheses
Func say2() (int, string) {return 1, "ok"}Copy the code
The return value is named
Func say3() (a int, func say3() (a int, // a = 18 b = "666" /* return (a); // a = 18 b = "666"Copy the code
The main function
Func main() {s := say1() FMT.Println(s) a1, b1 := say2() FMT.Println(a1, b1) a2, b2 := say3() FMT.Println(a2, b2)}Copy the code
conclusion
Above we learned the basic functions of Go. If you have any problems during the operation, please leave a comment in the discussion section below, we will solve the problem as soon as we see it.