- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我开始使用 go 工作了几个星期,并且(再一次)我偶然发现了一些对我来说似乎很奇怪的东西:
// Not working
a := 1
{
a, b := 2, 3
}
// Works
a := 1
a, b := 2, 3
我想同时分配两个变量。一个已经在上级范围内声明,另一个则没有。
它不起作用:编译器试图重新声明以前的变量。但是,如果在同一范围内声明此变量,则它可以正常工作。
这是为什么呢?
最佳答案
您所遇到的通常称为 "variable shadowing" .当您将 :=
与内部范围内的任何变量一起使用时,包括在像 if
和 for
这样的语句中,尽管缺少大括号,一个新值和类型与该变量相关联:
n := "Example"
//Prints the string variable `n` to standard output and
// returns the number of bytes written in int variable `n` and
// an error indicator in error variable `err`.
if n, err := fmt.Println(n); err != nil {
panic(err)
} else {
fmt.Println(n, "bytes written")
}
//Prints the string variable `n` to standard output.
fmt.Printf("n = %q\n", n)
输出:
Example
8 bytes written
n = "Example"
有几种不同的方法可以解决此问题:
=
:=
的变量名,并在范围结束之前恢复该值;使用不同的变量名通常更容易,因为无论如何您都在创建另一个变量也可能出现相反的效果,即您在内部范围内声明了某些内容而没有意识到它:
if _, err := fmt.Println(n); err != nil {
panic(err)
} else {
fmt.Println(n, "bytes written")
}
//undefined: err
if _, err = fmt.Println(n); err != nil {
//undefined: err
panic(err)
}
同样,有几种不同的方法可以解决此问题:
=
:=
和 if
语句分开,这样变量就按预期声明了;这允许您在该范围的上下文以及包含它的任何范围内对该变量的所有其他实例使用 =
=
的所有实例更改为 :=
以修复错误请注意,当函数返回多个值时,您可能会在最后两种情况下遇到变量阴影问题,但可以按照上述说明解决。
Try both examples on the Go Playground.
您的最后一个示例说明了声明和初始化新变量 b
的组合,同时还为现有变量 a
赋值。没有创建新的范围,因此您不会隐藏原始变量 a
,您可以 verify by printing the address of a
after each assignment (but before the next declaration/assignment) :
a := 1
fmt.Println(&a)
a, b := 2, 3
fmt.Println(&a)
a = b // avoids a "declared but not used" error for `b`
当然,如果你没有声明 b
,那么你会从编译器收到一个错误,即 :=
左侧没有新变量> 对于第二个声明,这是一种迂回的说法,表示您试图在同一范围内声明 a
两次。
请注意,如果仔细应用这个想法,也可以用来查找被遮蔽的变量。例如,您示例中的“不工作”代码将 print different addresses for a
,取决于内部范围内的 a
是否已经声明:
a := 1
{
fmt.Println(&a) // original `a`
a, b := 2, 3
fmt.Println(&a) // new `a`
a = b // avoids a "declared but not used" error for `b`
}
fmt.Println(&a) // original `a`
关于Golang 混合赋值和声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37351022/
我知道您不应该将打印与 printf,cout 和 wprintf,wcout 混合使用,但是很难找到一个好的答案,为什么以及是否可以绕过它。问题是我使用了一个用 printf 打印的外部库,而我自己
我有以下问题: class A: animal = 'gerbil' def __init__(self): self.result = self.calculate_
我在屏幕上渲染了一堆形状(多边形),我没有使用深度测试。 我只是希望这些形状在绘制在空白区域时使用自己的颜色,并且在绘制到任何非空区域时使用红色像素,即在我的情况下绘制在另一个多边形上。 这里的问题实
我正在尝试在我的 Groovy/Grails 应用程序中混入一个类,我正在使用 the syntax defined in the docs ,但我不断收到错误消息。 我有一个如下所示的域类: cla
我已经找到了 5349574673 个关于 Alpha 混合的页面,但我仍然无法获得想要的结果。我正在尝试使用 opengl 使 gif/png 文件正确显示(具有透明度/半透明度)。 这是我的初始化
我正在尝试记录以下代码,但我似乎无法让 JSDoc 记录该类,甚至无法引用它的存在。 // SomeMixin.js export default superclass => class SomeMi
我有一个类型家族,我想使用 mixin 以模块化方式“丰富”它们。例如: trait Family { self => trait Dog { def dogname:String
我在 Storyboard中有 Collection View 。我在 Storyboard中有一部分单元格,还有我以编程方式创建的部分单元格。我应该在 sizeForItemAtIndexPath
我有一个字节数组,我想更改它的访问方式。这是数组: char bytes[100]; 我想要另一个数组来改变原始数组的访问方式。如果我们可以将引用放在数组中,它看起来像这样: char& bytes_
我需要从 c 文件调用 cpp 方法。我为此编写了这个界面.. cpp文件 extern "C" void C_Test(int p){ Class::CPP_Test(p); } c文件
我的网站有两份 CSS 表,一份是主 CSS,一份是移动 CSS。问题是在移动设备(iPhone、Android)上查看时,两个样式表会混淆。例如,在 iPhone 上查看网站时,会应用主样式表中的某
维护人员的说明:此问题涉及已过时的 bokeh.charts API,该 API 已于多年前删除。有关使用现代 Bokeh 创建各种条形图的信息,请参阅: https://docs.bokeh.org
在下图中,蓝色圆圈仅用于调试目的。我的目标是蓝色圆圈后面的每一层都应该是透明的。我只想保持蓝色圆圈外面的可见。 这是用 swift 编写的代码: let croissantView = UIV
我不是 SQL 专家。我正在使用 SQL Server 2005,我正在尝试弄清楚如何构造一个查询,以便它可以满足多种要求。我有两个表定义如下: Classroom - ID - Departme
原创: 我之前问过这个问题,但我最初的例子有点不完整,我想我现在可以更具体地说明我的问题。 对于上下文,我在旧的 Apple mac 计算机上使用 openGL 3.3 并尝试渲染四边形的重叠层。每个
是否可以将内联(类似 json)映射与同一对象的常规映射定义混合使用? 考虑以下示例: person: {age: 32, weight: 82} name: foo 生成的人应具有给定的年龄、体
假设我有一个 Parent 类,它有四个字段 A、B、C 和 D,这样 C 和 D 可以选择传递或使用默认实现进行初始化: open class Parent(val a: A, val b: B,
我正在使用 symphony (1.4) 框架在 PHP 中开发一个 Web 应用程序。该代码使用 SVN 进行版本控制。在此网络应用程序中,我们所有客户共享一个共同的基础,以及一些专门为每个客户创建
我想使用两个小部件(一次一个)作为我的应用程序的基础/背景,上面有一个 QML UI 和一个无边框窗口。它应该看起来像这样: 基于 OpenGL 的扫描组件 通过窗口句柄操作的 3D 可视化组件 多个
我们有一个混合的 AngularJS/Angular 8 应用程序,并且我们不断遇到来自不同版本框架的组件之间的变化检测非常慢的问题。到目前为止,我们只在 Angular 组件中使用 AngularJ
我是一名优秀的程序员,十分优秀!