go 中的函数指针-ag捕鱼王app官网

当前位置:ag捕鱼王app官网 > > 编程语言 > >

go 中的函数指针

作者:迹忆客 最近更新:2023/04/26 浏览次数:

go语言中的指针是一种具有特殊用途的变量,用于存储其他变量的内存地址和内存所在的点。 也可以访问存储在该内存位置的值。


go 中的指针声明

var pointer_name *data_type

函数指针中使用的运算符

      • 它是一个声明指针变量的取消引用运算符。 可以使用它访问存储在解除引用地址中的值。
  1. & - 这是一个地址运算符,用于给出变量的地址。

在 go 中初始化一个指针

package main
import "fmt"
func main() {
    var myvariable int = 2
    var mypointer *int
    mypointer = &myvariable
    fmt.println("the value stored in 'myvariable' = ", myvariable)
    fmt.println("the address of 'myvariable' = ", &myvariable)
    fmt.println("the value stored in 'mypointer' = ", mypointer)
    fmt.println("the value pointed by 'mypointer' after dereferencing = ", *mypointer)
}

输出:

the value stored in 'myvariable' =  2
the address of 'myvariable' =  0xc000090020
the value stored in 'mypointer' =  0xc000090020.
the value pointed by 'mypointer' after dereferencing =  2

注意mypointer变量中存储的值是变量myvariable的地址。


在 go 中使用未初始化的指针

package main
import "fmt"
func main() {
    var mypointer *int
    fmt.println("the value stored in 'mypointer' = ", mypointer)
}

输出:

the value stored in 'mypointer' =  

请注意 ,未初始化的指针将具有默认的 nil 值。


在 go 中对指针使用简写 := 语法

package main
import "fmt"
func main() {
    myvariable := 2
    mypointer := &myvariable
    fmt.println("the value stored in 'myvariable' = ", myvariable)
    fmt.println("the address of 'myvariable' = ", &myvariable)
    fmt.println("the value stored in 'mypointer' = ", mypointer)
    fmt.println("the value pointed by 'mypointer' after dereferencing = ", *mypointer)
}

输出:

the value stored in 'myvariable' =  2
the address of 'myvariable' =  0xc000090020
the value stored in 'mypointer' =  0xc000090020
the value pointed by 'mypointer' after dereferencing =  2

请注意 ,如果您已使用指针声明指定数据类型,则您只能使用指针来处理该指定数据类型变量的内存地址。

:= 运算符自动识别数据类型并确保创建的指针可以处理相同的数据类型。


在 go 中使用指针更改变量的值

package main
import "fmt"
func main() {
    myvariable := 2
    mypointer := &myvariable
    fmt.println("the value stored in 'myvariable' = ", myvariable)
    fmt.println("the address of 'myvariable' = ", &myvariable)
    fmt.println("the value stored in 'mypointer' = ", mypointer)
    fmt.println("the value pointed by 'mypointer' after dereferencing = ", *mypointer)
    *mypointer = 5
    fmt.println("the updated value pointed by 'mypointer' after dereferencing = ", *mypointer)
    fmt.println("the updated value stored in 'myvariable' = ", myvariable)
}

输出:

the value stored in 'myvariable' =  2
the address of 'myvariable' =  0xc000016058
the value stored in 'mypointer' =  0xc000016058
the value pointed by 'mypointer' after dereferencing =  2
the updated value pointed by 'mypointer' after dereferencing =  5
the updated value stored in 'myvariable' =  5

请注意,您可以更改指针的值而不是为变量分配新值。 使用指针更改的值也反映在原始值中。


在 go 中使用 new() 函数创建指针

package main
import "fmt"
func main() {
    var mypointer = new(int)
    *mypointer = 2
    fmt.println("the value stored in 'mypointer' = ", mypointer)
    fmt.println("the value pointed by 'mypointer' after dereferencing = ", *mypointer)
}

输出:

the value stored in 'mypointer' =  0xc000016058
the value pointed by 'mypointer' after dereferencing =  2

请注意 ,使用 new() 函数,您可以直接创建一个指向整数的指针。


将指针作为参数传递给 go 中的函数

package main
import "fmt"
func myfunction(myvariable *int) {
    *myvariable = 5
}
func main() {
    var myvariable = 2
    fmt.printf("the value of myvariable before function call is: %d\n", myvariable)
    var mypointer *int = &myvariable
    myfunction(mypointer)
    fmt.printf("the value of myvariable after function call is: %d\n", myvariable)
}

输出:

the value of myvariable before function call is: 2
the value of myvariable after function call is: 5

请注意,上面创建的函数更改了变量 myvariable 的值。 函数中改变的值反映在函数外,因为指针直接引用变量存储的内存位置。


将变量的地址作为参数传递给 go 中的函数

package main
import "fmt"
func myfunction(myvariable *int) {
    *myvariable = 5
}
func main() {
    var myvariable = 2
    fmt.printf("the value of myvariable before function call is: %d\n", myvariable)
    myfunction(&myvariable)
    fmt.printf("the value of myvariable after function call is: %d\n", myvariable)
}

输出:

the value of myvariable before function call is: 2
the value of myvariable after function call is: 5

请注意 ,上面创建的函数更改了变量 myvariable 的值。 函数中更改的值反映在函数外部。

更改是对存储变量的内存位置进行的。 这是将指针传递给函数的另一种方法。


从 go 中的函数返回指针

package main
import "fmt"
func main() {
  displaytext := display()
  fmt.println("hello,", *displaytext)
}
func display() *string {
  mymessage := "world!"
  return &mymessage
}

输出:

hello, world!

请注意 ,上面创建的函数将返回一个指向字符串 mymessage 的指针。 然后在主函数中取消引用该值。

上一篇:

下一篇:

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

发布时间:2023/04/27 浏览次数:101 分类:

本篇文章介绍了在 golang 的 html 模板中使用 if-else 和 switch 循环。因此,只要输出是 html,就应该始终使用 html 模板包而不是文本模板。

发布时间:2023/04/27 浏览次数:184 分类:

本篇文章介绍 nil 在 golang 中的含义,nil 是 go 编程语言中的零值,是众所周知且重要的预定义标识符。

golang 中的 lambda 表达式

发布时间:2023/04/27 浏览次数:691 分类:

本篇文章介绍如何在 golang 中创建 lambda 表达式。lambda 表达式似乎不存在于 golang 中。 函数文字、lambda 函数或闭包是匿名函数的另一个名称。

发布时间:2023/04/27 浏览次数:136 分类:

当我们尝试生成对象的副本时,深层副本会准确复制原始对象的所有字段。 此外,如果它有任何对象作为字段,也会制作这些对象的副本。本篇文章介绍如何在 golang 中进行深度复制。

发布时间:2023/04/27 浏览次数:104 分类:

像错误一样,panic 发生在运行时。 换句话说,当您的 go 程序中出现意外情况导致执行终止时,就会发生 panics。让我们看一些例子来捕捉 golang 中的panics。

go 中的日志级别

发布时间:2023/04/27 浏览次数:585 分类:

本篇文章介绍如何在 golang 中创建和使用日志级别。go 中的日志级别。golang提供了一个日志包,名为log,是一个简单的日志包。 这个包不提供分级日志; 如果我们想要分级日志记录,我们必须

在 go 中使用断言

发布时间:2023/04/27 浏览次数:585 分类:

本篇文章介绍了 assert 在 golang 中的使用。在 go 语言中使用断言:golang 不提供对断言的任何内置支持,但我们可以使用来自 testify api 的广泛使用的第三方包断言。

go 中的随机数生成

发布时间:2023/04/27 浏览次数:206 分类:

本篇文章介绍如何在 go 语言中使用随机数生成功能。go 中的随机数生成 go 语言为随机数生成功能提供内置支持。 内置包 math 有方法 rand(),用于随机数生成。

golang 电子邮件验证器

发布时间:2023/04/27 浏览次数:300 分类:

本篇文章介绍如何在 go 语言中验证电子邮件。电子邮件需要特定格式; 否则,它们将无法工作。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图