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

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

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)