1

I'd like to provide a similar interface than the one provided by a map in which you can get the value related to the key and know if the key was present in the map, but my attempts at it have failed so far.

Sample code Golang's play version:

package main

import "fmt"

type My map[string]string

func (m My) Get(key string) (string, bool) {
    v, ok := m[key]
    return v, ok
}

func main() {
    my := make(My)
    a := my.Get("hola")
    fmt.Println("result : ", a)
}

This code will fail with:

prog.go:14: multiple-value my.Get() in single-value context

How can one mimic the sommeMap[key]? is it a built-in feature only?

icza
  • 389,944
  • 63
  • 907
  • 827
Willyfrog
  • 475
  • 1
  • 5
  • 12
  • Damn, I looked, but couldn't find your second link, which would then resolve my doubts. Thanks – Willyfrog Sep 03 '15 at 10:26
  • Your Get method is returning 2 values (string, bool) but at line 14 you are expecting only one value a := my.Get("hola"). Replace line 14 with a,_ := my.Get("hola"). This should resolve your issue – Prashant Thakkar Sep 03 '15 at 11:47
  • Prashant Thakkar, thanks but that's not the issue really. I know I can get a second one, but I want to be able to ignore as you can ignore the second parameter when asking for a value at a key in a map. – Willyfrog Sep 03 '15 at 13:13

0 Answers0