T
T
to_east2018-03-14 14:45:10
go
to_east, 2018-03-14 14:45:10

Structures, pointers, arrays?

Greetings Toaster Members!
Please explain some things. So I need to make an array of structures, and each element of the array had an action, there seems to be some kind of magic with pointers, which I haven’t felt yet. In general code:

package main

import "fmt"


type nextWords struct {
    words []string
}

func (nw *nextWords) GetFirstItem() string {
    return nw.words[0]
}

func main() {
    var myMap = make(map[string]nextWords)
    myMap["hello"] = nextWords{words: []string{"foo", "bar"}}

    fmt.Println(myMap["hello"].GetFirstItem())
}

The compiler complains about:
C:\Windows\system32\cmd.exe /c (go run test1.go ^& pause)
# command-line-arguments
.\test1.go:18: cannot call pointer method on myMap["hello"]
.\test1.go:18: cannot take the address of myMap["hello"]
Для продолжения нажмите любую клавишу . . .

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Pavlyuk, 2018-03-14
@to_east

In this case, you need to put pointers to your structure in the map.
https://play.golang.org/p/Jn18TTSDh5T

package main

import "fmt"

type nextWords struct {
  words []string
}

func (nw *nextWords) GetFirstItem() string {
  return nw.words[0]
}

func main() {
  var myMap = make(map[string]*nextWords)
  myMap["hello"] = &nextWords{words: []string{"foo", "bar"}}

  fmt.Println(myMap["hello"].GetFirstItem())
}

The second option is to declare the method to be called on a value rather than a pointer.
Your GetFirstItem method is declared to be called on a pointer (nw *nextWords), so it can only be called on an element from which it can get an address. It is impossible to take it from the map because when we take it, we get a copy of this element, and not the element itself. Inplace go cannot take a pointer to a structure in this case, unless we first pull it out of the map into a variable and call a method on it ( for example, like this )
When we make a map from pointers, we get a copy of the pointer from it, not the structure itself, and we can call a method on this pointer.
But keep in mind that if there is no element in the map, your program will panic, so you should check for nil in the GetFirstItem method.

T
to_east, 2018-03-14
@to_east

Alright, thanks for the detailed explanation!
It turns out that you need to create an array of pointers to the nextWords structure.
unless we first pull it out of the map into a variable and call a method on it
By the way, I just managed to do this, based on this I wanted to make a call (&myMap["hello"]).GetFirstItem() of this kind, but the file also came out

R
Roman, 2018-03-15
@KirEv

why map? more context, is it for yourself or part of another task?
I suppose that with this array of structures you will need to perform some actions, if there is no need for an associative array, in my opinion, it is better not to use map
without mapping

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question