草庐IT

go - 函数返回的结构在它所属的数组中没有改变

coder 2024-07-13 原文

我正在用 Gotk3 编写一个小型 GUI 应用程序,这是我在伪代码中的基本设置:

type Point struct {
    X float64
    Y float64
    IsSelected bool
}

func getClosestElement(pT []Point, p Point, maxDist float64) Point {
    /* returns the point from pT with the minimum distance to p */
}

func main() {
    // GTK init ..
    selectedPoints := make([]Point, 0)

    /* GTK-Event onMouseClick */
    {
        /* if left mouse click */
        selectedPoints = append(selectedPoints, Point{X: event.x, Y: event.y})
        /* if right mouse click */
        closestPoint = getClosestElement(selectedPoints, Point{X: event.x, Y: event.y}, 15.0)
        closestPoint.IsSelected = true
    }

    /* GTK-Event Draw */
    {
        /* Loop over all selectedPoints */
        if selectedPoint.IsSelected
        /* Draw point in blue and also print to console, if a selected point was detected */
        else
        /* Draw point in black */
    }
}

但不知何故,即使从 selectedPoints 获取最近的 Point 工作正常,我也从未获得控制台输出以循环遍历 IsSelected 属性为 true 的点,我也没有得到一个蓝色的绘制点(这表明该点已被选中)。

这给我留下了一个问题,即返回的结构(它是最初提供给函数的 slice 的一部分)实际上是按值而不是按引用返回的。所以改变这个返回的点不会改变它以前所属的数组中的点?

最佳答案

如果将非指针值压入 slice ,然后函数从该 slice 返回一个值,它将是该值的副本。因此,如果您更改该值的属性,它不会影响 slice 中的值。

您可以:

  • 制作你的Point指针值,例如*Point
  • 有一个名为 selectClosestElement 的函数,它将 IsSelected 设置为 true 并替换 slice 中的值。

例子:

// With pointers
// GTK init ..
    selectedPoints := make([]*Point, 0)

    /* GTK-Event onMouseClick */
    {
        /* if left mouse click */
        selectedPoints = append(selectedPoints, &Point{X: event.x, Y: event.y})
        /* if right mouse click */
        closestPoint = getClosestElement(selectedPoints, &Point{X: event.x, Y: event.y}, 15.0)
        closestPoint.IsSelected = true
    }
// With a selectClosestPoint function replacing the value in the slice
// GTK init ..
    selectedPoints := make([]Point, 0)
    /* GTK-Event onMouseClick */
    {
        /* if left mouse click */
        selectedPoints = append(selectedPoints, Point{X: event.x, Y: event.y})
        /* if right mouse click */
        selectClosestPoint(selectedPoints, Point{X: event.x, Y: event.y}, 15.0)
    }
...

func selectClosestPoint(selectedPoints []Point, point Point, maxDist float64) {
    for i, sPoint := range selectedPoints {
        // ...
        // this is the correct point
        sPoint.IsSelected = true
        // Replace it in the slice
        selectedPoints[i] = sPoint
    }
}

关于go - 函数返回的结构在它所属的数组中没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55318548/

有关go - 函数返回的结构在它所属的数组中没有改变的更多相关文章

  1. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  2. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  3. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  4. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  5. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  6. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  7. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  8. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  9. ruby - 检查数组是否在增加 - 2

    这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife

  10. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

随机推荐