- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
var person struct {
name string
id int
phone int
}
func main () {
var myTest person
//construct the variable
...
go func() {
fmt.Println(myTest.name)
}()
}
goroutine 是否从 main 函数深度复制变量“myTest”?
如果我的 goroutine 是这样的:
go func() {
time.Sleep(10 * time.Second)
fmt.Println(myTest.name)
}
这个 goroutine sleep 10 秒,那么当 main 函数在 10 秒内改变“myTest”的值时,goroutine 会做什么?
最佳答案
go没有“深拷贝”的概念,一切都是按值传递。
你的样本甚至不是浅拷贝你传递了一个指针(字符串地址):
如果您在 main
函数中更改 myTest.name
然后在更改后再次打印它,您会看到它会发生变化,请参阅此证明示例代码:
package main
import (
"fmt"
"sync"
"time"
)
type person struct {
name string
id int
phone int
}
var wg sync.WaitGroup
func main() {
myTest := person{"Alex", 22, 123}
fmt.Printf("%T : %[1]v %v\n", myTest.name, &myTest.name) // string : Alex 0xc042006740
wg.Add(1)
go func() {
fmt.Printf("%T : %[1]v %v\n", myTest.name, &myTest.name) // string : Alex 0xc042006740
time.Sleep(500 * time.Millisecond)
fmt.Printf("%T : %[1]v %v\n", myTest.name, &myTest.name) // string : J 0xc042006740
wg.Done()
}()
time.Sleep(100 * time.Millisecond)
myTest.name = "J"
wg.Wait()
}
首先像这样定义person struct
:
type person struct {
name string
id int
phone int
}
第二次使用 sync.WaitGroup
等待 goroutine 完成。
关于你的主要问题,你可以这样自己测试:
package main
import (
"fmt"
"sync"
"time"
)
type person struct {
name string
id int
phone int
}
var wg sync.WaitGroup
func main() {
myTest := person{"Alex", 22, 123}
wg.Add(1)
go func() {
fmt.Printf("%T : %[1]v\n", myTest.name) // string : Alex
time.Sleep(500 * time.Millisecond)
fmt.Printf("%T : %[1]v\n", myTest.name) // string : J
wg.Done()
}()
time.Sleep(100 * time.Millisecond)
myTest.name = "J"
wg.Wait()
}
因此,正如您在此示例中看到的,main 函数中的字符串 name
内容更改反射(reflect)到 goroutine,因此它不是副本。
如果您需要这样的复制调用:
package main
import (
"fmt"
"sync"
"time"
)
type person struct {
name string
id int
phone int
}
var wg sync.WaitGroup
func main() {
myTest := person{"Alex", 22, 123}
wg.Add(1)
go func(name string) {
fmt.Printf("%T : %[1]v\n", name) // string : Alex
time.Sleep(500 * time.Millisecond)
fmt.Printf("%T : %[1]v\n", name) // string : Alex
wg.Done()
}(myTest.name)
time.Sleep(100 * time.Millisecond)
myTest.name = "J"
wg.Wait()
}
关于go - goroutine创建的是深拷贝还是浅拷贝?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37983977/
我有一个基类和两个派生类,我需要将一个指向派生类对象的指针复制到另一个类中,就像示例一样。 class Base { public: Base(const Base& other); } cl
考虑 Container 类,它主要存储 Box 对象的 unique_ptr vector ,并可以对它们执行一些计算。 class Container { private: std::
引用是指保存的值为对象的地址。在 Python 语言中,一个变量保存的值除了基本类型保存的是值外,其它都是引用,因此对于它们的使用就需要小心一些。下面举个例子: 问题描述:已知一个列表,求生成一个
我正在尝试实现 Bron-Kerbosch 算法,这是一种用于查找派系的递归算法。我设法达到了一个点,它返回了正确数量的派系,但是当我打印它们时,它们不正确 - 添加了额外的节点。我在这里遗漏了什么明
在评估中,我选择了选项LINE I 上的运行时错误。没有未定义行为这样的选项,尽管我认为这是正确的选择。 我不确定,但我认为评估有误。我编译并运行了该程序,它确实打印了 3, 9, 0, 2, 1,
在函数签名中通过 const 值传递参数是否有任何好处(或相反,成本)? 所以: void foo( size_t nValue ) { // ... 对比 void foo( const s
我为 answer to another question 写了一个 OutputIterator .在这里: #include using namespace std; template clas
我有一个由第三方生成的 dll,它具有某种内部数据结构,将其大小限制为 X 个元素。 所以基本上,它有一个以 X 为限制的队列。 据我所知,DLL 是每个进程的,但是是否可以多次加载 DLL?也许每个
假设我有以下两个数据结构: std::vector all_items; std::set bad_items; all_items vector 包含所有已知项和 bad_items vector
如何在不渲染 CGImage 的情况下从另一个 CIImage 复制一个 CIImage 最佳答案 CIImage *copiedImage = [originalImage copy]; 正如您在
我有一个名为 UINode 的 GUI,我想创建一个拷贝并只更改一些内容。该项目由 3 个基本线程组成。 PingThread、RosThread 和 GuiThread。我试图复制粘贴项目文件夹并将
Qt 新手。如果这个问题太幼稚,请多多包涵。在 Windows 操作系统环境中,我有 Qt 对话框框架应用程序,它具有“重复”- 按钮。在同一目录中,有 Qt 应用程序 - (一个带有关闭按钮的对话框
我正在尝试创建一个函数来复制我的卡片结构。我只需复制 cvalue 即可轻松开始。然而,我的 cvalue 没有复制,当应该读取 1000 时它仍然读取 5。 #include #include
string str1("someString"); string str2 = string(str1);//how many copies are made here //copy2 =
我希望了解 boost::bind 执行何种函数对象的内部拷贝。由于这些对象的构造函数似乎没有被调用,我推测这是一种“非常浅的复制”,所以我引入了动态内存分配来产生一些错误。但是,下面代码的运行时输出
我正在查看 http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c22-make-default-operations-consis
下面的类方法Augmented3dPoint::getWorldPoint()返回对其成员的引用 cv::Point3f world_point; class Augmented3dPoint { p
我需要通过 MyClass2 将用户定义的 lambda 传递给 MyClass1。我想确保只有一步,没有拷贝。下面的代码实现了吗?有没有更好的方法来做到这一点(比如使用编译器完成的隐式移动)? 注意
在我的数据库访问代码中,我想写一个方法: variant_t GetQueryRows (...) 我想这样调用它: const variant_t result = GetQueryRows (..
我有一个包含引用的类,例如: class A { A(B &b) : b(b) {} // constructor B &b; } 有时b必须是只读的,有时是可写的。当我创建一个 const A
我是一名优秀的程序员,十分优秀!