Golang implements set set, which realizes slicing and de-weight in disguise

Source: Internet
Author: User
Tags bool set set sort

The original address: http://www.jb51.net/article/56828.htm, this article extracts the set of the part, and the small harmless error has been modified

Java has set set, and Golang No, today suddenly there is a need to a bunch of int slices to combine to heavy, depressed for a long time, their soil method is too high complexity, see the above post feel good happy:

The map in Golang is not allowed to be duplicated, see this code

M: = map[string]string{
		"1": "One",
		"2": "One",
		"3": "Three",
	}
	fmt. PRINTLN (m)//output Map[1:one 2:two 3:three]
If I had written this way,
M: = map[string]string{
		"1": "One",
		"2": "One", "1": "One
		",
		"3": "Three",
	}
	fmt. Println (M)
Program Direct error: Duplicate key "1" in map literal through the small example above, the Golang map is not allowed to repeat the key, detailed description see HTTP://GOLANGHOME.COM/POST/155 and http://www . Tuicool.com/articles/rrinzv above is not the point. Then, according to the map's characteristics, you can create a set of Java sets, thanks again for the original http://www.jb51.net/article/56828.htm Where I added a sort of return result, though it just called a system function, but it felt good. Run Test passed
Package main import ("FMT" "Sort" "sync") type Set struct {m map[int]bool sync. Rwmutex} func New () *set {return &set{m:map[int]bool{},}} func (S *set) ADD (item int) {S.lock () defer s . Unlock () S.m[item] = true} func (S *set) Remove (item int) {S.lock () defer s.unlock () Delete (S.M, Item)} func (S * Set) has (item int) bool {s.rlock () defer S.runlock () _, OK: = S.m[item] return OK} func (S *set) Len () int {retur N Len (S.list ())} func (S *set) Clear () {s.lock () defer s.unlock () S.M = map[int]bool{}} func (S *set) IsEmpty () Bo  OL {if s.len () = = 0 {return true} return false} func (s *set) list () []int {s.rlock () defer s.runlock () List : = []int{} for item: = Range S.M {list = append (list, item)} return list} func (S *set) sortlist () []int {S.rlo CK () defer s.runlock () List: = []int{} for item: = Range S.M {list = append (list, item)} sort. Ints (list) return list} func main () {//Initialize S: = New () S.add (1) S.A.DD (1) s.add (0) S.add (2) S.add (4) S.add (3) s.clear () if S.isempty () {fmt. Println ("0 item")} s.add (1) s.add (2) S.add (3) if S.has (2) {FMT. Println ("2 does Exist")} s.remove (2) s.remove (3) fmt. Println ("unordered slices", s.list ()) fmt.
 PRINTLN ("Ordered slices", s.sortlist ())}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.