Go 맵에 key 있는지 확인

Jmnote (토론 | 기여)님의 2023년 2월 28일 (화) 10:27 판 (→‎개요)

1 개요

Go 맵에 key 있는지 확인
package main

import "fmt"

func main() {
	var m = map[string]string{}
	m["a"] = "hello"
	m["b"] = "world"
	m["c"] = "lorem"

	var ok bool
	if _, ok = m["c"]; ok {
		fmt.Println("✔️ exists")
	} else {
		fmt.Println("❌ not exists")
	}

	if _, ok = m["d"]; ok {
		fmt.Println("✔️ exists")
	} else {
		fmt.Println("❌ not exists")
	}

}

2 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}