Package introduction, function and variable usage in go language

Release: 2019-11-29 14:27:00
forward
3049 people have browsed it

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"
)
Copy after login

or

import "fmt"
import "math"
Copy after login

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)  //正确引入,首字母要大写
}
Copy after login

, 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;
}
Copy after login

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)
}
Copy after login

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;
}
Copy after login

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
}
Copy after login

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)
}
Copy after login

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!

Related labels:
source:csdn.net
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!