set container

630 단어 golanggolang

There is no built-in set container in Go

How to implement Set
struct{} => type
struct{}{} => 0bytes

type Set map[interface{}]struct{}

func (s Set) Insert(v interface{}) {
	s[v] = struct{}{}
}
func (s Set) Delete(v interface{}) {
	delete(s, v)
}
func (s Set) Find(v interface{}) bool {
	_, ok := s[v]
	return ok
}
func (s Set) Size() int {
	return len(s)
}
func (s Set) Clear() {
	for i := range s {
		delete(s, i)
	}
}

How to create
set := Set{} or set := make(Set)

Environment

reference

좋은 웹페이지 즐겨찾기