#Map in outer code block is nil

19 messages · Page 1 of 1 (latest)

hardy elm
#

Code can be found here but pasting in here as well.

// You can edit this code!
// Click here and start typing.
package main

import (
    "os"
    "path/filepath"
)

func main() {
    sourcePath := "foobar"
    var repos []*config.GitRepo
    var sourceRepos = make(map[string]*config.GitSource)
    err := filepath.Walk(sourcePath,
        func(path string, info os.FileInfo, err error) error {
            if err != nil {
                return err
            }
            //some condition met.
            repos = append(repos, newRepo)
            if source, ok := sourceRepos[remoteURL]; ok {
                sourceName = source.Name
            } else {
                newSource := &config.GitSource{
                    Name: slug.Make(remoteURL),
                    Uri:  remoteURL,
                }
                if slices.Contains([]string{"master", "main"}, s.Name().String()) {
                    newSource.DefaultBranch = s.Name().String()
                }
                sourceName = newSource.Name
                sourceRepos[remoteURL] = newSource
            }

        })
    if err != nil {
        // at this point, repos is valid, but sourceRepos is nil.  Why is that?
    }

    return nil
}


teal verge
#

if you're not assigning nil to sourceRepos then sourceRepos can't be nil

#

to be clear, there's another issue here, either with your understanding or with your observation

hardy elm
#

that's the code, nothing special it's just pretty long code that's irrelevant tot he question. THe code I added is the only real write operations

#

My assumption that if i update sourceRepos from main, inside the lambda function that it should still have those values unless it only has access to a copy of it

teal verge
#

pretty long code that's irrelevant tot he question
given the facts, it's hard to trust this 🙂

#

note: when you ask a question it means you don't know the answer, if you delete code under the assumptions you hold, you can't really be sure if it's relevant or not, otherwise there wouldn't be a problem

#

so, just paste the full thing

hardy elm
teal verge
#

how are you arriving at the conclusion that sourceRepos is nil?

hardy elm
#

updated the link

#

because my debugger code claims it is

#

So, if I use &map, that works, but I'm trying to understand why I would need that

#

Okay, never mind. Sorry @teal verge . Looks like it was my IDE that was just being dumb

teal verge
#

could you tell me what precisely you're talking about? are talking main? are we talking ImportRepos?
I would suggest adding a fmt.Println(myThing == nil where you claim the issue is

hardy elm
#

now it's working and naturally with no rhyme or reason as it's the same code

teal verge
#

ah ok

#

I noticed you were using *map, this is rarely a necessary choice.
Maps act like reference types, copying a map (such as passing it as a parameter to another function) does actually copy the map. In other words a map is a pointer already

hardy elm
#

Yeah, that was my work around to get it "fixed" but then undoing also made it work, so rolled back that minor change