为什么结构体字面量被称为“字面量”?

huangapple go评论81阅读模式
英文:

Why are struct literals "literal"

问题

在golang中,结构体字面量是什么?

为什么即使有一个变量,下面的代码仍然被称为字面量?即使不是常量,结构体不是本质上就是变量吗?这怎么说得通呢?

MyType{Field: var)

它有一个变量,但它仍然被称为“字面量”?

另外,为什么只有在首次初始化时才称为“结构体字面量”?

英文:

In golang what are struct literals?

Why is the following a literal even though there is a variable? And aren't structs literally variable, even when not const? So how does it make sense.

MyType{Field: var)

It has a variable and yet it's a "literal"?

Also why is it only called a "struct literal" when you first initialize it?

答案1

得分: 28

编程语言在提到构建某些数据结构的语法方式时使用“字面量”一词。这意味着它不是通过创建一个空的数据结构,然后逐步添加或减去来构建的。

比较一下:

MyType{Field: myVariable}

var x = new(MyType)
x.Field = myVariable

好处是你的代码的外观反映了某种数据结构。缺点是你必须事先知道布局并且已经初始化了内容,如果你正在构建一个具有未知键的映射,这是不可能的。

以下是Go语言规范中字面量的链接。请注意,它们都是定义数据结构的语法方式:

英文:

Programming languages use the word "Literal" when referring to syntactic ways to construct some data structure. It means it's not constructed by creating an empty one and adding or subtracting as you go.

Compare:

MyType{Field: myVariable}

to

var x = new(MyType)
x.Field = myVariable

The benefit is that your code's appearance reflects the data structure in some way. The downside is that you have to know the layout in advance and have the content initialized already, not possible, if for instance, you're constructing a map with unknown keys.

Here are links to the literals in the Go language specification. Notice that they all are syntactic ways to define a data structure:

答案2

得分: 0

literal 指的是语法或者组成源代码的方式,即按照源代码的字面意义构造特定类型的值。

int/string literal 恰好构造了一个与源代码的字面意义相同的常量值。

根据 Go 语言规范

> 每次计算时,复合字面量都会构造新的复合值。

从它们的定义来看,这是一种为复合类型构造值的语法糖。它不是literal wiki page 中提到的固定值

引用自《Go 程序设计语言》4.4.1 节关于 struct literal 的内容:

> 可以使用结构体字面量来写一个结构体类型的值,为它的字段指定相应的值。

type Point struct{ X, Y int }
p := Point{1, 2}

string literalsstruct literals 都是源代码,用于指导编译器在内存中构造值。struct literals 可以在编译时非常量化,即在运行时才知道。

还有另一种类型的字面量:type literal

> 也可以使用类型字面量来指定一个类型,它由现有类型组成。

这意味着在源代码中为类型系统提供了一个固定值

struct{}
struct{ foo int32 }
英文:

literal just means the grammar or the way to compose source code, i.e., construct a value of a certain type following the literal meaning of its source code.

int/string literal happens to construct a constant value that is the same as the literal meaning of the source code.

from go spec

> Composite literals construct new composite values each time they are evaluated.

from their definition, it's syntactic sugar to construct value for the composite type. It's not the fixed value referred by literal wiki page.

cited from gopl 4.4.1 for struct literal:

> A value of a struct type can be written using a struct literal that specifies values for its fields.

type Point struct{ X, Y int }
p := Point{1, 2}

Both string literals and struct literals are source codes to guide the compiler to construct value in memory. struct literals can be non-constant at compile time, i.e., only known at runtime.

there's also another type of literal: type literal:

> A type may also be specified using a type literal, which composes a type from existing types.

which just means a fixed value in the source code for the type system.

struct{}
struct{ foo int32 }

huangapple
  • 本文由 发表于 2015年3月21日 02:44:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/29173720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定