博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Golang学习日志 ━━ 函数传递指针参数的语法糖误区
阅读量:4116 次
发布时间:2019-05-25

本文共 572 字,大约阅读时间需要 1 分钟。

一句话,函数传入指针参数可用语法糖省掉,但目前版本只针对结构体有效,如下*

package mainimport (	"fmt")func main() {
s1 := 2 fmt.Println("p1:", p1(&s1)) //p1: 3 s2 := 2 p2(&s2) fmt.Println("p2:", s2) //p2: 3 var s3 ss1 s3.b = 2 p3(&s3) fmt.Println("p3:", s3) //p3: 4}func p1(x *int) int {
fmt.Println(x) fmt.Println(*x) (*x)++ //x++ //(*x 语法糖无法使用) return *x}func p2(x *int) {
fmt.Println(x) fmt.Println(*x) *x = 20 //x++ //(*x 语法糖无法使用)}func p3(x *ss1) {
fmt.Println(x) fmt.Println(*x) (*x).b++ x.b++ //(*x 语法糖可用)}type ss1 struct {
a string b int c []int d map[string]int}

所以建议还是用完整方式写,远离某些糖

转载地址:http://vvkpi.baihongyu.com/

你可能感兴趣的文章
数据结构之队列、栈
查看>>
数据结构之树
查看>>
数据结构之二叉树
查看>>
二叉树非递归遍历算法思悟
查看>>
红黑树算法思悟
查看>>
从山寨Spring中学习Spring IOC原理-自动装配注解
查看>>
实例区别BeanFactory和FactoryBean
查看>>
Spring后置处理器BeanPostProcessor的应用
查看>>
Spring框架的ImportSelector到底可以干嘛
查看>>
Mysql中下划线问题
查看>>
微信小程序中使用npm过程中提示:npm WARN saveError ENOENT: no such file or directory
查看>>
Xcode 11 报错,提示libstdc++.6 缺失,解决方案
查看>>
idea的安装以及简单使用
查看>>
Windows mysql 安装
查看>>
python循环语句与C语言的区别
查看>>
Vue项目中使用img图片和background背景图的使用方法
查看>>
vue 项目中图片选择路径位置static 或 assets区别
查看>>
vue项目打包后无法运行报错空白页面
查看>>
Vue 解决部署到服务器后或者build之后Element UI图标不显示问题(404错误)
查看>>
element-ui全局自定义主题
查看>>