When to use unnamed type?

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

When to use unnamed type?

问题

阅读答案

我们使用命名的结构类型,例如,

type Foo struct{
   name string
   email string
}

var y Foo

用于解码传输的数据或引入一个新类型,该类型包含一组值及对这些值进行操作的操作


1)何时使用无名类型?

             var x struct{
                 name string
                 email string
              }

2)初始化变量标识符x的语法是什么?

英文:

Read the answer

We use named struct type, say,

type Foo struct{
   name string
   email string
}

var y Foo

to decode the data coming on wire or introduce a new type that holds set of values and operations on those values


  1. When to use unnamed type?

              var x struct{
                  name string
                  email string
               }
    
  2. What is the syntax to initialize variable identifier x?

答案1

得分: 2

作为一个快速的规则,如果你发现自己在复制/输入结构体定义,那么最好将其定义为一个命名类型。毕竟,命名的目的就是为了多次引用同一事物而提供的快捷方式。为了避免命名混乱,尽量在最窄的范围内声明类型(例如,在使用它的函数范围内)。


初始化的语法如下:

var x struct{
	name string
	email string
} = struct {
	name  string
	email string
}{
	name: "name", 
	email: "email",
}

可以简化为

var x = struct {
	name  string
	email string
}{
	name:  "name",
	email: "email",
}

因为类型可以被推断出来。

在不需要显式引用类型的情况下,使用无名类型是最合适的时机。由于类型没有名称,要显式引用它,你(通常)必须再次复制整个类型规范。例如,在分配新值时。

// 重新分配 x
x = struct {
	name  string
	email string
}{
	name:  "name2",
	email: "email2",
}

这会很快变得非常丑陋,特别是对于较大的结构体,而且重复的代码在需要更新时会创建重复的工作量。

你可以尝试将其简化如下:

// 重新分配 x
x.name = "name2"
x.email = "email2"

然而,这应该被视为不好的形式,因为不清楚意图是重新分配整个值"x"还是只修改一些选择的字段。如果结构体更新以添加更多字段,那么这段代码可能会在没有警告的情况下变得不正确。

英文:

As a quick rule, if you ever find yourself copying/typing out the struct definition again, you may as well make it a named type. That is the whole point of names, after all; they are a shortcut to refer to the same thing multiple times. To avoid naming clutter, try to declare the type in the narrowest scope possible (for example, within the function scope where it is used).


The syntax to initialize is as follows:

var x struct{
	name string
	email string
} = struct {
	name  string
	email string
}{
	name: "name", 
	email: "email",
}

which can be shortened to

var x = struct {
	name  string
	email string
}{
	name:  "name",
	email: "email",
}

since the type can be inferred.

The best time to use unnamed types is when you do not have to explicitly refer to the type again. Since the type has no name, to refer to it explicitly you (generally) have to duplicate the whole type specification again. For example, when assigning a new value.

// reassign x
x = struct {
	name  string
	email string
}{
	name:  "name2",
	email: "email2",
}

This will quickly get quite ugly, especially for larger structs, and duplicated code creates duplicated work when it needs to be updated.

You could attempt to shorten this as follows:

// reassign x
x.name = "name2"
x.email = "email2"

This should be considered bad form, however, as it is unclear if/that the intent was to reassign the entire value of "x" or just alter some select fields. If the struct is updated to add more fields, then this code may become incorrect without warning.

huangapple
  • 本文由 发表于 2022年3月31日 04:34:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/71683815.html
匿名

发表评论

匿名网友

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

确定