前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >go 何时无法取地址

go 何时无法取地址

作者头像
平凡的学生族
发布2019-05-25 09:03:24
4740
发布2019-05-25 09:03:24
举报
文章被收录于专栏:后端技术后端技术

why-cant-i-get-the-address-of-a-type-conversion-in-go

The Go Programming Language Specification

Expressions An expression specifies the computation of a value by applying operators and functions to operands.

Conversions Conversions are expressions of the form T(x) where T is a type and x is an expression that can be converted to type T.

Address operators For an operand x of type T, the address operation &x generates a pointer of type *T to x. The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressable struct operand; or an array indexing operation of an addressable array. As an exception to the addressability requirement, x may also be a (possibly parenthesized) composite literal. If the evaluation of x would cause a run-time panic, then the evaluation of &x does too.

Expressions are temporary, transient values. The expression value has no address. It may be stored in a register. A comversion is an expression. For example,

代码语言:javascript
复制
package main

import (
    "fmt"
)

func main() {
    type str string
    s := "hello, world"
    fmt.Println(&s, s)

    // error: cannot take the address of str(s)
    sp := &str(s)
    fmt.Println(sp, *sp)
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.03.31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档