From 84df183d0ee92cef35114a75d009f940f514acb7 Mon Sep 17 00:00:00 2001 From: RickyWang1020 <50431019+RickyWang1020@users.noreply.github.com> Date: Fri, 28 Aug 2020 11:04:21 +0800 Subject: [PATCH] Update golang_tutorial_15.md --- docs/golang_tutorial_15.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/docs/golang_tutorial_15.md b/docs/golang_tutorial_15.md index 0773d65..7c95573 100644 --- a/docs/golang_tutorial_15.md +++ b/docs/golang_tutorial_15.md @@ -156,3 +156,31 @@ value of b is 255 new value of b is 256 ``` +## 将指针传入函数 + +```golang +package main + +import ( + "fmt" +) + +func change(val *int) { + *val = 55 +} +func main() { + a := 58 + fmt.Println("value of a before function call is",a) + b := &a + change(b) + fmt.Println("value of a after function call is", a) +} +``` + +在第 14 行,我们将存有 `a` 的地址的指针 `b` 传入了 `change` 函数。`change` 函数通过间接引用的方法改变了 `a` 的值,程序输出: + +```golang +value of a before function call is 58 +value of a after function call is 55 +``` +