go-syncmap

command module
v1.2.113 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2024 License: MIT Imports: 17 Imported by: 0

README

Build Status GoDoc Report card Sourcegraph

go-syncmap

Generates Go code using a package as a generic template for sync.Map.

go-syncmap Generates Go code using a package as a generic template for sync.Map. Given the name of a sync.Map type T , and the name of a type Key and Value go-syncmap will create a new self-contained Go source file implementing

// type T sync.Map
// T<Key,Value>

# from Go version 1.9 onward 
func (m *T) Load(key Key) (Value, bool)
func (m *T) Store(key Key, value Value)
func (m *T) LoadOrStore(key Key, value Value) (Value, bool)
func (m *T) Delete(key Key)
func (m *T) Range(f func(key Key, value Value) bool

# from Go version 1.15 onward 
func (m *T) LoadAndDelete(key Key) (Value, bool)

The file is created in the same package and directory as the package that defines T, Key and Value. It has helpful defaults designed for use with go generate.

For example, given this snippet,

package painkiller

import "sync"

type Pill sync.Map

running this command

go-syncmap -type="Pill<int,string>"

in the same directory will create the file pill_syncmap.go, in package painkiller, containing a definition of


# from Go version 1.9 onward 
func (m *Pill) Store(key int, value string)
func (m *Pill) LoadOrStore(key int, value string) (string, bool)
func (m *Pill) Load(key int) (string, bool)
func (m *Pill) Delete(key int)
func (m *Pill) Range(f func(key int, value string) bool)

# from Go version 1.15 onward 
func (m *Pill) LoadAndDelete(key int) (string, bool)

Typically this process would be run using go generate, like this:

//go:generate go-syncmap -type "Pill<int, string>"
//go:generate go-syncmap -type "Pill<int, time.Time>"
//go:generate go-syncmap -type "Pill<int, encoding/json.Token>"

If multiple constants have the same value, the lexically first matching name will be used (in the example, Acetaminophen will print as "Paracetamol").

With no arguments, it processes the package in the current directory. Otherwise, the arguments must name a single directory holding a Go package or a set of Go source files that represent a single Go package.

The -type flag accepts a comma-separated list of types so a single run can generate methods for multiple types. The default output file is t_syncmap.go, where t is the lower-cased name of the first type listed. It can be overridden with the -output flag.

Download/Install

The easiest way to install is to run go get install github.com/searKing/golang/tools/go-syncmap . You can also manually git clone the repository to $GOPATH/src/github.com/searKing/golang/tools/go-syncmap.

Inspiring projects

Documentation

Overview

go-syncmap Generates Go code using a package as a generic template for sync.Map. Given the name of a sync.Map type T , and the name of a type Key and Value go-syncmap will create a new self-contained Go source file implementing from Go version 1.9 onward

func (m *T) Store(key Key, value Value)
func (m *T) LoadOrStore(key Key, value Value) (Value, bool)
func (m *T) Load(key Key) (Value, bool)
func (m *T) Delete(key Key)
func (m *T) Range(f func(key Key, value Value) bool

from Go version 1.15 onward

func (m *T) LoadAndDelete(key Key) (Value, bool)

The file is created in the same package and directory as the package that defines T, Key and Value. It has helpful defaults designed for use with go generate.

For example, given this snippet,

package painkiller

import "sync"

type Pill sync.Map

running this command

go-syncmap -type Pill<int,time.Time>

in the same directory will create the file pill_syncmap.go, in package painkiller, containing a definition of

from Go version 1.9 onward

func (m *Pill) Store(key int, value time.Time)
func (m *Pill) LoadOrStore(key int, value time.Time) (time.Time, bool)
func (m *Pill) Load(key int) (time.Time, bool)
func (m *Pill) Delete(key int)
func (m *Pill) Range(f func(key int, value time.Time) bool

from Go version 1.15 onward

func (m *Pill) LoadAndDelete(key int) (string, bool)

Typically this process would be run using go generate, like this:

//go:generate go-syncmap -type Pill<int, string>
//go:generate go-syncmap -type Pill<int, time.Time>
//go:generate go-syncmap -type Pill<int, encoding/json.Token>

With no arguments, it processes the package in the current directory. Otherwise, the arguments must name a single directory holding a Go package or a set of Go source files that represent a single Go package.

The -type flag accepts a comma-separated list of types so a single run can generate methods for multiple types. The default output file is t_string.go, where t is the lower-cased name of the first type listed. It can be overridden with the -output flag.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL