当前位置:网站首页>[golang] quick review guide quickreview (IV) -- functions
[golang] quick review guide quickreview (IV) -- functions
2022-06-23 20:22:00 【DDGarfield】
function
C# The function in
1. Variable parameters params
static int IntSum(int x, int y)
{
return x + y;
}
// heavy load , Variable parameters
static int IntSum(params int[] x)
{
int sum = 0;
for (int i = 0; i < x.Length; i++)
{
sum += x[i];
}
return sum;
}
2. Return multiple values
2.1 Parameters out
static void Calc(int x, int y, out int sum, out int sub)
{
sum = x + y;
sub = x - y;
}
2.2 Tuple- Tuples
// Tuples
// static (int, int) Calc(int x, int y)
static (int sum, int sub) Calc(int x, int y)
{
return (x + y, x - y);
}
3.delegate entrust ( Function type )
C# The function type in is a delegate
public delegate (int, int) CalculationHandler(int x, int y);
// Use
static void TestDelegate(CalculationHandler handler)
{
}
// Anonymous functions Yes delegate keyword , No function signature
TestDelegate(delegate (int x, int y)
{
return (x + y, x - y);
});
// Call to transfer value ,lambda expression , hold delegate Omit directly
TestDelegate((x, y) =>
{
return (x + y, x - y);
});
Golang The function in
Golang The return value of is after the function name and parameters , Functions must be annotated , And start with the function name , otherwise golint There will be a green prompt
1. Variable parameters ...
// IntSum Write notes
func IntSum(x int, y int) int {
return x + y
}
// go Function overloading is not supported , Therefore, the function signatures in the same package are not consistent
func Sum(x ...int) int {
// here x Is an array
sum := 0
for _, v := range x {
sum = sum + v
}
return sum
}
2. Return multiple values
See more forms , If for no other , Just to understand the source code of others
func Calc(x int, y int) (sum int,sub int) {
sum = x + y
sub = x - y
return sum, sub
}
func Calc(x int, y int) ( int, int) {
sum := x + y
sub := x - y
return sum, sub
}
func Calc(x, y int) (int, int) {
sum := x + y
sub := x - y
return sum, sub
}
3. Function type
Function as a type ,C# Created the Commission , Looks more elegant , and Golang The definition of is more direct :
type calculation func(int, int) (int, int)
3.1 Use
func main() {
var calc calculation
calc = addOrSub
sum, sub := calc(5, 6)
fmt.Printf("result:sum-%v sub-%v \n", sum, sub)
sum1, sub2 := testFuncType(8, 6, addOrSub)
fmt.Printf("result:sum-%v sub-%v \n", sum1, sub2)
}
type calculation func(int, int) (int, int)
func addOrSub(x, y int) (int, int) {
return x + y, x - y
}
// testFuncType This is more like a commission Action Func<> Use
func testFuncType(x, y int, op calculation) (int, int) {
return op(x, y)
}
3.2 Anonymous functions
Golang Anonymous functions of are not like C# like that , need delegate, It is without any keywords , The use is also simple and rough :
addOrSubTest := func(x, y int) (int, int) {
return x + y, x - y
}
sum3, sub3 := addOrSubTest(9, 6)
fmt.Printf("anonymous function:sum-%v sub-%v \n", sum3, sub3)
4. The return value is the function
Since the function is a type , It can also be used as the return value of the function :
func do(s string) func(int, int) int {
switch s {
case "+":
return func(x int, y int) int {
return x + y
}
case "-":
return func(x int, y int) int {
return x - y
}
default:
return nil
}
}
5. New content defer
defer Statement will delay the statement that follows it . The function is about to return, Press the deferred statement to defer The definition is executed in reverse order , in other words , First define defer The statement of is finally executed , Finally defined defer The sentence of , The first to perform . Like a stack , First in, then out , Last in, first out .
func main() {
var calc calculation
calc = addOrSub
sum, sub := calc(5, 6)
fmt.Printf("result:sum-%v sub-%v \n", sum, sub)
}
type calculation func(int, int) (int, int)
func addOrSub(x, y int) (int, int) {
defer fmt.Println("addOrSub1")
defer fmt.Println("addOrSub2")
defer fmt.Println("addOrSub3")
fmt.Printf("result:%v and %v\n", x+y, x-y)
return x + y, x - y
}
result:11 and -1
addOrSub3
addOrSub2
addOrSub1
result:sum-11 sub--1
More complicated ( Cheating father ) Of defer Example , Please refer to some interview questions .
Again : This series is not a tutorial , If you want to learn systematically , Bloggers can recommend learning resources .
边栏推荐
- Official announcement. Net 7 preview 5
- The evolution of the "Rainbow Bridge" middleware platform for the acquisition database based on shardingsphere
- Yaokui tower in Fengjie, Chongqing, after its completion, will be the safety tower for Sichuan river shipping with five local scholars in the company
- Kubernetes resource topology aware scheduling optimization
- After the collapse of UST, will the stable currency market pattern usher in new opportunities?
- 35 year old crisis? It has become a synonym for programmers
- Rstudio 1.4 software installation package and installation tutorial
- [golang] reexamine closures from the perspective of go language
- Why is only one value displayed on your data graph?
- Kinsoku Jikou Desu Sina stock interface change
猜你喜欢

Activity registration | introduction to mongodb 5.0 sequential storage features

技术分享| WVP+ZLMediaKit实现摄像头GB28181推流播放

LeetCode 1079. movable-type printing

35岁危机?内卷成程序员代名词了…

GL Studio 5 安装与体验

硬件开发笔记(六): 硬件开发基本流程,制作一个USB转RS232的模块(五):创建USB封装库并关联原理图元器件

I came from a major, so I didn't want to outsource

JDBC 在性能測試中的應用

Kubernetes 资源拓扑感知调度优化

盘点四种WiFi加密标准:WEP、WPA、WPA2、WPA3
随机推荐
UST 崩盘后,稳定币市场格局将迎来新机遇?
Elastricearch's fragmentation principle of the second bullet
小程序开发框架推荐
如何利用数仓创建时序表
Use of the vs2022scanf function. An error is reported when using scanf - the return value is ignored: Solutions
Emmet语法规范
【Golang】类型转换归纳总结
国元期货交易软件正规吗?如何安全下载?
【Golang】怎样优雅的清空切片
基于微信小程序的婚纱影楼小程序开发笔记
Daily question brushing record (II)
Flagai Feizhi: AI basic model open source project, which supports one click call of OPT and other models
每日刷题记录 (二)
20省市公布元宇宙路线图
【Golang】来几道题以加强Slice
ZABBIX monitoring - Aruba AP operation data
怎么开户?在国海证券开户安全吗?需要带什么?
Logstash start -r parameter
【Golang】跟着源码学技巧系列之对象池sync.Pool
[golang] reexamine closures from the perspective of go language