Documentation
¶
Overview ¶
Package gowords provides words table and text resource.
Index ¶
- func FindBy(w Words, name string, suffix core.Suffix) (string, bool)
- func GetBy(w Words, name string, suffix core.Suffix) string
- type DoAnnotation
- func (w DoAnnotation) FindFormatted(name string, arguments ...any) (string, bool)
- func (w DoAnnotation) FindIndexed(name string, arguments ...any) (string, bool)
- func (w DoAnnotation) FindNamed(name string, arguments map[string]any) (string, bool)
- func (w DoAnnotation) GetFormatted(name string, arguments ...any) string
- func (w DoAnnotation) GetIndexed(name string, arguments ...any) string
- func (w DoAnnotation) GetNamed(name string, arguments map[string]any) string
- type WithSuffix
- type Words
- type WordsCollection
- type WordsFile
- type WordsRepository
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FindBy ¶ added in v1.1.0
FindBy a helper to search for a name by suffix and using Words object, then return value and `true` if found, else return empty string and `false`. Also return empty string and `false` if suffix is invalid.
Example ¶
package main import ( "fmt" gowords "github.com/saleh-rahimzadeh/go-words" "github.com/saleh-rahimzadeh/go-words/core" ) func main() { const source = ` k1_EN = v1 EN k2_EN = v2 EN k1_FA = v1 FA k2_FA = v2 FA ` var words gowords.Words words, err := gowords.NewWordsRepository(source, core.Separator, core.Comment) if err != nil { panic(err) } const ( EN core.Suffix = "_EN" FA core.Suffix = "_FA" ) valueEn, foundEn := gowords.FindBy(words, "k1", EN) if foundEn { fmt.Println(valueEn) } valueFa, foundFa := gowords.FindBy(words, "k1", FA) if foundFa { fmt.Println(valueFa) } }
Output: v1 EN v1 FA
func GetBy ¶ added in v1.1.0
GetBy a helper to search for a name by suffix and using Words object, then return value if found, else return empty string. Also return empty string if suffix is invalid.
Example ¶
package main import ( "fmt" gowords "github.com/saleh-rahimzadeh/go-words" "github.com/saleh-rahimzadeh/go-words/core" ) func main() { const source = ` k1_EN = v1 EN k2_EN = v2 EN k1_FA = v1 FA k2_FA = v2 FA ` var words gowords.Words words, err := gowords.NewWordsRepository(source, core.Separator, core.Comment) if err != nil { panic(err) } const ( EN core.Suffix = "_EN" FA core.Suffix = "_FA" ) valueEn := gowords.GetBy(words, "k1", EN) fmt.Println(valueEn) valueFa := gowords.GetBy(words, "k1", FA) fmt.Println(valueFa) }
Output: v1 EN v1 FA
Types ¶
type DoAnnotation ¶ added in v1.1.0
type DoAnnotation struct {
Words
}
DoAnnotation utilize Words interface to provide words table and text resource and format value according to an annotation or a format specifier
Example ¶
package main import ( "fmt" gowords "github.com/saleh-rahimzadeh/go-words" "github.com/saleh-rahimzadeh/go-words/core" ) func main() { const source = ` person_named=Hi, my name is {{name}}, when I was {{age}} years old I was a {{language}} developer. person_indexed=Hi, my name is {{1}}, when I was {{2}} years old I was a {{3}} developer. person_formatted=Hi, my name is %s, when I was %d years old I was a %v developer. ` var wRepository gowords.Words wRepository, err := gowords.NewWordsRepository(string(source), core.Separator, core.Comment) if err != nil { panic(err) } words, err := gowords.NewDoAnnotation(wRepository) if err != nil { panic(err) } value1person := words.GetNamed("person_named", map[string]any{ "name": "Saleh", "age": "15", "language": "Assembly", }) fmt.Println(value1person) value2person, found_person := words.FindNamed("person_named", map[string]any{ "name": "Saleh", "age": "17", "language": "Pascal", }) if found_person { fmt.Println(value2person) } value1personindexed := words.GetIndexed("person_indexed", "Saleh", "19", "C++") fmt.Println(value1personindexed) value2personindexed, found_personindexed := words.FindIndexed("person_indexed", "Saleh", "23", "CSharp") if found_personindexed { fmt.Println(value2personindexed) } value1personformatted := words.GetFormatted("person_formatted", "Saleh", 32, "JavaScript") fmt.Println(value1personformatted) value2personformatted, found_personformatted := words.FindFormatted("person_formatted", "Saleh", 36, "Golang") if found_personformatted { fmt.Println(value2personformatted) } }
Output: Hi, my name is Saleh, when I was 15 years old I was a Assembly developer. Hi, my name is Saleh, when I was 17 years old I was a Pascal developer. Hi, my name is Saleh, when I was 19 years old I was a C++ developer. Hi, my name is Saleh, when I was 23 years old I was a CSharp developer. Hi, my name is Saleh, when I was 32 years old I was a JavaScript developer. Hi, my name is Saleh, when I was 36 years old I was a Golang developer.
func NewDoAnnotation ¶ added in v1.1.0
func NewDoAnnotation(words Words) (DoAnnotation, error)
NewDoAnnotation create a new instance of NewDoAnnotation
func (DoAnnotation) FindFormatted ¶ added in v1.1.0
func (w DoAnnotation) FindFormatted(name string, arguments ...any) (string, bool)
FindFormatted search for a name then return value and `true` if found, else return empty string and `false`. Format value with formatted verbs according to "https://pkg.go.dev/fmt#hdr-Printing".
func (DoAnnotation) FindIndexed ¶ added in v1.1.0
func (w DoAnnotation) FindIndexed(name string, arguments ...any) (string, bool)
FindIndexed search for a name then return value and `true` if found, else return empty string and `false`. Format value with indexed annotations.
func (DoAnnotation) FindNamed ¶ added in v1.1.0
FindNamed search for a name then return value and `true` if found, else return empty string and `false`. Format value with named annotations.
func (DoAnnotation) GetFormatted ¶ added in v1.1.0
func (w DoAnnotation) GetFormatted(name string, arguments ...any) string
GetNamed search for a name then return value if found, else return empty string. Format value with formatted verbs according to "https://pkg.go.dev/fmt#hdr-Printing".
func (DoAnnotation) GetIndexed ¶ added in v1.1.0
func (w DoAnnotation) GetIndexed(name string, arguments ...any) string
GetIndexed search for a name then return value if found, else return empty string. Format value with indexed annotations.
type WithSuffix ¶ added in v1.1.0
type WithSuffix struct { Words // contains filtered or unexported fields }
WithSuffix utilize Words interface with suffix to provide categorized words table and text resource
Example ¶
package main import ( "fmt" gowords "github.com/saleh-rahimzadeh/go-words" "github.com/saleh-rahimzadeh/go-words/core" ) func main() { const source = ` k1_EN = v1 EN k2_EN = v2 EN k1_FA = v1 FA k2_FA = v2 FA ` var wRepository gowords.Words wRepository, err := gowords.NewWordsRepository(source, core.Separator, core.Comment) if err != nil { panic(err) } const ( EN core.Suffix = "_EN" FA core.Suffix = "_FA" ) wordsEN, err := gowords.NewWithSuffix(wRepository, EN) if err != nil { panic(err) } wordsFA, err := gowords.NewWithSuffix(wRepository, FA) if err != nil { panic(err) } value1en := wordsEN.Get("k1") fmt.Println(value1en) value2en, found2en := wordsEN.Find("k2") if found2en { fmt.Println(value2en) } value1fa := wordsFA.Get("k1") fmt.Println(value1fa) value2fa, found2fa := wordsFA.Find("k2") if found2fa { fmt.Println(value2fa) } }
Output: v1 EN v2 EN v1 FA v2 FA
func (WithSuffix) Find ¶ added in v1.1.0
func (w WithSuffix) Find(name string) (string, bool)
Find search for a name with suffix then return value and `true` if found, else return empty string and `false`
func (WithSuffix) Get ¶ added in v1.1.0
func (w WithSuffix) Get(name string) string
Get search for a name with suffix then return value if found, else return empty string
type Words ¶
type Words interface { // Get search for a name then return value if found, else return empty string Get(string) string // Find search for a name then return value and `true` if found, else return empty string and `false` Find(string) (string, bool) }
Words the interface to specify required methods to get and find words
type WordsCollection ¶
type WordsCollection struct {
// contains filtered or unexported fields
}
WordsCollection provide words table and text resource with accepting string source and storing in map
Example ¶
package main import ( "fmt" gowords "github.com/saleh-rahimzadeh/go-words" ) func main() { const separator rune = '=' const comment rune = '#' const source = ` k1=v1 k2=v2 k3=v3 ` w, err := gowords.NewWordsCollection(source, separator, comment) if err != nil { panic(err) } value_1, found := w.Find("k1") if found { fmt.Println(value_1) } value_2 := w.Get("k1") fmt.Println(value_2) }
Output: v1 v1
func NewWordsCollection ¶
func NewWordsCollection(source string, separator rune, comment rune) (WordsCollection, error)
NewWordsCollection create a new instance of WordsCollection
func (WordsCollection) Find ¶
func (w WordsCollection) Find(name string) (string, bool)
Find search for a name then return value and `true` if found, else return empty string and `false`
func (WordsCollection) Get ¶
func (w WordsCollection) Get(name string) string
Get search for a name then return value if found, else return empty string
type WordsFile ¶
type WordsFile struct {
// contains filtered or unexported fields
}
WordsFile provide words table and text resource with accepting file pointer and storing a pointer to the file
Example ¶
file, err := os.Open(path.Join(path_WORDS, "valid__want")) if err != nil { panic(err) } defer file.Close() const separator rune = '=' const comment rune = '#' w, err := gowords.NewWordsFile(file, separator, comment) if err != nil { panic(err) } err = w.CheckError() if err != nil { panic(err) } value_1, found := w.Find("k1") if found { fmt.Println(value_1) } var value_2 string = w.Get("k1") fmt.Println(value_2)
Output: v1 v1
func NewWordsFile ¶
NewWordsFile create a new instance of WordsFile
func (*WordsFile) CheckError ¶
CheckError check errors in file. Also check for duplication of names.
func (WordsFile) Find ¶
Find search for a name then return value and `true` if found, else return empty string and `false`. It is safe for concurrent use by multiple goroutines.
func (*WordsFile) FindUnsafe ¶
FindUnsafe search for a name then return value and `true` if found, else return empty string and `false`. It is unsafe for concurrent use by multiple goroutines.
type WordsRepository ¶
type WordsRepository struct {
// contains filtered or unexported fields
}
WordsRepository provide words table and text resource with accepting string source and storing in array
Example ¶
package main import ( "fmt" gowords "github.com/saleh-rahimzadeh/go-words" ) func main() { const separator rune = '=' const comment rune = '#' const source = ` k1=v1 k2=v2 # comment k3=v3 ` w, err := gowords.NewWordsRepository(source, separator, comment) if err != nil { panic(err) } value := w.Get("k1") fmt.Println(value) }
Output: v1
Example (CustomComment) ¶
package main import ( "fmt" gowords "github.com/saleh-rahimzadeh/go-words" "github.com/saleh-rahimzadeh/go-words/core" ) func main() { const comment rune = '@' const source string = ` k1=v1 @ this is a comment k2=v2 ` w, err := gowords.NewWordsRepository(source, core.Separator, comment) if err != nil { panic(err) } value := w.Get("k1") fmt.Println(value) }
Output: v1
Example (CustomSeparator) ¶
package main import ( "fmt" gowords "github.com/saleh-rahimzadeh/go-words" "github.com/saleh-rahimzadeh/go-words/core" ) func main() { const separator rune = ':' const source string = ` k1:v1 k2:v2 k3:v3 ` w, err := gowords.NewWordsRepository(source, separator, core.Comment) if err != nil { panic(err) } value := w.Get("k1") fmt.Println(value) }
Output: v1
func NewWordsRepository ¶
func NewWordsRepository(source string, separator rune, comment rune) (WordsRepository, error)
NewWordsRepository create a new instance of WordsRepository
func (WordsRepository) Find ¶
func (w WordsRepository) Find(name string) (string, bool)
Find search for a name then return value and `true` if found, else return empty string and `false`
Example ¶
package main import ( "fmt" gowords "github.com/saleh-rahimzadeh/go-words" ) func main() { const separator rune = '=' const comment rune = '#' const source = ` k1=v1 k2=v2 k3=v3 ` w, err := gowords.NewWordsRepository(source, separator, comment) if err != nil { panic(err) } value, found := w.Find("k1") if found { fmt.Println(value) } }
Output: v1
func (WordsRepository) Get ¶
func (w WordsRepository) Get(name string) string
Get search for a name then return value if found, else return empty string