FAQ
I'm trying to figure out why this is not working:

package main

import "fmt"

func main() {
a := []string{"zero", "one", "two"}
fmt.Println(a) // outputs: [zero one two]
v, ok := a[1] // error: assignment count mismatch: 2 = 1
fmt.Printf("%s %t", v, ok)
}
http://play.golang.org/p/I_-7B0m_5V

The spec clearly says:

An index expression on a map a of type map[K]V may be used in an assignment
or initialization of the special form

v, ok = a[x]
v, ok := a[x]
var v, ok = a[x]

where the result of the index expression is a pair of values with types (V,
bool). In this form, the value of ok is true if the key x is present in the
map, and false otherwise. The value of v is the value a[x] as in the
single-result form.
http://golang.org/ref/spec#Indexes

I'm trying it to use it to check if a key exists in a slice, but I can't
seem to solve this. What am I doing wrong?

--

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 20, '13 at 7:39a
activeJan 20, '13 at 11:20a
posts9
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase