github.com/enetx/g@v1.0.80/examples/maps.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/enetx/g"
     7  	"github.com/enetx/g/pkg/ref"
     8  )
     9  
    10  func main() {
    11  	gos := g.NewMap[string, *int]()
    12  
    13  	gos.GetOrSet("root", ref.Of(3))
    14  	fmt.Println(*gos.Get("root").Some() == 3)
    15  
    16  	*gos.GetOrSet("root", ref.Of(10)) *= 2
    17  	fmt.Println(*gos.Get("root").Some() == 6)
    18  
    19  	//////////////////////////////////////////////////////////////////////////
    20  
    21  	gos2 := g.NewMap[int, g.Slice[int]]()
    22  
    23  	for i := range 5 {
    24  		gos2.Set(i, gos2.Get(i).UnwrapOrDefault().Append(i))
    25  	}
    26  
    27  	for i := range 10 {
    28  		gos2.Set(i, gos2.Get(i).UnwrapOrDefault().Append(i))
    29  	}
    30  
    31  	gos2.Print()
    32  
    33  	//////////////////////////////////////////////////////////////////////////
    34  
    35  	god := g.NewMap[int, g.Slice[int]]()
    36  
    37  	for i := range 10 {
    38  		god[i] = god.Get(i).UnwrapOrDefault().Append(i)
    39  	}
    40  
    41  	for i := range 10 {
    42  		god[i] = god.Get(i).UnwrapOrDefault().Append(i)
    43  	}
    44  
    45  	god.Print()
    46  }