当前位置:网站首页>Self study golang [3.4go language functions and pointers] define a function that returns one or more values. For the pointer of go language, the pointer cannot operate. For the parameter transmission
Self study golang [3.4go language functions and pointers] define a function that returns one or more values. For the pointer of go language, the pointer cannot operate. For the parameter transmission
2022-07-22 08:35:00 【Aspire to be a doctor】
Catalog
1.1 Define a function to return a value
1.2 Define a function to return multiple values
The function returns 2 It's worth , How to accept only one value ?
1.go Functions of language
1.1 Define a function to return a value
Code section :
func eval(a, b int, op string) int { // Defined function eval,a,b Is a shaping parameter ,op Is a string parameter ,int Is the return value of the function
switch op {
case "+":
return a + b
case "-":
return a - b
case "*":
return a * b
case "/":
return a / b
default:
panic("unsupported op:" + op) // Used for error reporting
}
}
Run a screenshot :
1.2 Define a function to return multiple values
Code section :
func div(a, b int) (q, r int) {
return a / b, a % b
}
The function returns 2 It's worth , How to accept only one value ?
func main() {
fmt.Println(eval(1, 2, "+"))
q, r := div(13, 3)
fmt.Println(q, r)
// The function returns 2 It's worth , How to accept only one value ?
a, _ := div(13, 3) // answer : Replace another value with an underscore
fmt.Println(a)
}
Run a screenshot :
Function code summary :
package main
import "fmt"
// The definition function returns a value
func eval(a, b int, op string) int { // Defined function eval,a,b Is a shaping parameter ,op Is a string parameter ,int Is the return value of the function
switch op {
case "+":
return a + b
case "-":
return a - b
case "*":
return a * b
case "/":
return a / b
default:
panic("unsupported op:" + op) // Used for error reporting
}
}
// Define a function to return multiple values
func div(a, b int) (q, r int) {
return a / b, a % b
}
func main() {
fmt.Println(eval(1, 2, "+"))
q, r := div(13, 3)
fmt.Println(q, r)
// The function returns 2 It's worth , How to accept only one value ?
a, _ := div(13, 3) // answer : Replace another value with an underscore
fmt.Println(a)
}
2.go The pointer to language , A pointer cannot operate ,go Language parameter passing has only value passing , Address passing through pointers
Code section :
package main
import "fmt"
//go Language pointer , A pointer cannot operate
//go The parameter passing of language is only value passing , Address passing through pointers
func swap(a, b *int) {
*b, *a = *a, *b
// Or write directly as return b,a
}
func main() {
var a int = 2
var pa *int = &a
*pa = 3
fmt.Println(a)
aa, bb := 1, 2
swap(&aa, &bb)
fmt.Println(aa, bb)
}
Run a screenshot :
End of this chapter
边栏推荐
- C语言指针和数组a与&a,&a[0]等的区别
- Verilog -- 74hc85 four bit numeric comparator and extended to 16 bit numeric comparator
- Multiple worksheets under the same workbook have the same format
- 自学golang【3.7map的练习代码】
- September memo
- 量化交易中如何判断趋势
- How excel automatically matches gender code according to ID number
- 職業交易者自用多年的終極突破策略分享(附全套交易模板)
- try catch finally 中包含return的几种情况,及返回结果
- Analysis of location cache module and detailed explanation of OCP monitoring and alarm
猜你喜欢
不断提升认知,从而达到交易的最高级别——稳定盈利(终)
十年交易员重磅推荐:简单易操作的突破回调策略
Detailed construction process of airflow (personal test + summary)
NOR FLASH 和 NAND FLASH异同
量化交易日记-2021年02月总结
AttributeError:module ‘distutils’ has no attribute ‘version错误解决方法
Hide and unhide Excel
Redis的经典三问以及哨兵
SPI debugging is not successful, it is likely that you connected the wrong cable!!
Pytorch learning (I): linear regression
随机推荐
Some skills of Excel
地址总线、数据总线、控制总线详细解释
House sequencing
线程池7个参数的含义
胜率高达93.98%的TPS交易策略
The use of comma expression in C language
房屋排序法
p[0]作为for循环的判断条件
在 EXCEL 中,“插入已剪切单元格”的快捷键
TPS trading strategy with a winning rate of 93.98%
Verilog应用——24秒篮球计数器
Pytorch learning (I): linear regression
WebSockets and server sent events
Spark read CSV file operation, explanation of option parameter
Can you do application development without programming? Yes!
Anaconda installs jupyter lab + jupyterlsp (code prompt, code error correction) detailed construction process
EXCEL一些技巧
9月备忘录
Percona xtradb cluster installation
Continuously improve cognition, so as to reach the highest level of trading - stable profits (I)