dsn5510 2016-11-17 07:15
浏览 24
已采纳

无法访问在另一个函数中更改的结构字段

I m beginner in go lang. I have created an object array of a struct in function list and pass the address of that object array to another function changelist.In my function changelist i get it as pointer.I wanted to edit some of the fields of passed array object inside this function changedlist and pass it back to the called function list.When i try to access it in my called function i cant access the updated version from the changedlist function.

Here is my code
type Mail struct {
        Name    string
        Address string
        Status  bool
        Error   error
    }
function List(){
          var addressList = make([]Mail,2)
          addressList[0].Name = "Name"
          addressList[0].Address = "Address"
          addressList[1].Name = "Name1"
          addressList[1].Address = "Address"
          newAddress:=changedlist(&addressList)
          fmt.Println(newAddress)
}
function Changedlist(addressList *[]Mail)*[]Mail{
            for _,r:=range *addressList{
                    if r.Name=="Name1"{
                          r.Status=false
                           r.err=errors.New("Error in the name")
                            continue
                        }
                      r.Status=true
                      r.Error=nil
                 }
           return addressList
   }

Here what i was expecting is for name, the status will become true when i get this in my list function.But it is false which is the default value of the bool.This means that the changes are not done to the actual array struct.It is modifying the copied version.Can someone please point me in the right direction how to achieve this..Thanks.

  • 写回答

1条回答 默认 最新

  • douleng0794 2016-11-17 07:44
    关注
    package main
    
    import (
        "errors"
        "fmt"
    )
    
    type Mail struct {
        Name    string
        Address string
        Status  bool
        Error   error
    }
    
    func List() {
        var addressList = make([]Mail, 2)
        addressList[0].Name = "Name"
        addressList[0].Address = "Address"
        addressList[1].Address = "Address"
        addressList[1].Name = "Name1"
        fmt.Println(addressList)
        newAddress := Changedlist(&addressList)
        fmt.Println(newAddress)
    }
    func Changedlist(addressList *[]Mail) *[]Mail {
        for k, r := range *addressList {
            if r.Name == "Name1" {
                r.Status = false
                r.Error = errors.New("Error in the name")
                (*addressList)[k] = r
                continue
            }
            r.Status = true
            r.Error = nil
            (*addressList)[k] = r
        }
        return addressList
    }
    
    func main() {
        List()
    }
    
    // output
    [{Name Address false <nil>} {Name1 Address false <nil>}]
    &[{Name Address true <nil>} {Name1 Address false Error in the name}]
    

    This is my code, it's not perfect but works correctly.
    There are some things you got wrong.

    1. "function" is not a keyword, you should use "func" instead.
    2. You call the function Changedlist with changedlist.
    3. in for loop, the variable r is a copy of the array item, so if you changed it, the array is not affected, I used (*addressList)[k] = r here to change the array. Also, you could modify it with index like (*addressList)[k].Name = xxx without using r

    That's all
    Hope this can help you ...

    https://play.golang.org/p/zAJ5aJpwwA

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度