Home  >  Article  >  Backend Development  >  Package introduction, function and variable usage in go language

Package introduction, function and variable usage in go language

尚
forward
2019-11-29 14:27:003061browse

Package introduction, function and variable usage in go language

Package introduction, function and variable examples in go language:

1. Go package introduction

The introduction of go is introduced through the import keyword

For example:

import (
	"fmt"
	"math"
)

or

import "fmt"
import "math"

go package introduction, when calling methods or variables, the first letter is required It can only be called in uppercase letters, such as

package main
 
import "fmt"
import "math"
func main() {
	//以下代码不可执行,供参考
	fmt.Println(math.pi) //错误引入
	fmt.Println(math.Pi)  //正确引入,首字母要大写
}

, that is:

Package introduction, function and variable usage in go language

## 2. Go function

1. Return value

package main
 
func main() {
	//调用函数
	result := myFun(1, 1)
	println(result)
}
 
//自定义函数
//x,y为参数,函数末尾int为返回值
func myFun(x, y int) int {
	return x+y;
}

2. No return value

package main
 
func main() {
	//调用函数
	myVoidFun(1, 1)
}
 
//自定义函数
//x,y为参数
func myVoidFun(x, y int)  {
	println("函数myVoidFun被调用了")
	println("传入的参数x、y分别是:")
	print(x)
	print("、")
	print(y)
}

3. Multiple return values ​​

package main
 
func main() {
	//调用函数
	myVoidFun(1, 1)
}
 
//自定义函数
//x,y为参数
func myVoidFun(x, y int)  {
	a, b := multiple(1, 2)
	println("第一个参数:")
	println(a)
	println("第二个参数:")
	println(b)
}
 
/**
多个返回值
前边为入参
返回值为列表(int,int)
 */
func multiple(x , y int)(int,int){
	return x,y;
}

4. Named return value

package main
 
import "strconv"
 
func main() {
	//调用函数
	result, resultStr := myReturnFun(1, 2)
	println("返回第一个参数")
	println(result)
	println("返回第二参数")
	println(resultStr)
}
//命名返回值
func myReturnFun(x , y int) (result int,resultStr string) {
	println("执行函数mrReturnFun")
	result = x //直接作为第一个返回值
	resultStr = strconv.Itoa(y)//直接作为第二个返回值
	return
}

Named return value:

refers to writing the returned value directly in the function body, and then just return it directly. The advantage of this is that it can reduce a certain amount of code and try to use it in a method with shorter logic. If the method If the body is too long, this method is not recommended.

3. Variables

var: can be declared in the function body or outside the function body

package main
 
import "strconv"
//函数体外声明
var param1,param2,param3 string
func main() {
	var  value1, value2,value3 int;
	//赋值函数体外参数
	param1="is param1"
	param2="is param2"
	param3="is param3"
	//方法体内赋值
	value1 = 1
	value1 = 2
	value1 = 3
	println("函数体外的参数")
	println(param1)
	println(param2)
	println(param3)
	println("函数体内的参数")
	println(value1)
	println(value2)
	println(value3)
}

The above is the detailed content of Package introduction, function and variable usage in go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete