Documentation ¶
Overview ¶
Package utils provides utility functions for Mybot.
Index ¶
- func CalcBools(b1, b2, add bool) bool
- func CalcStringSlices(s1, s2 []string, add bool) []string
- func CheckStringContained(ss []string, str string) bool
- func Decode(ext string, bs []byte, v interface{}) error
- func DecodeFile(file string, v interface{}) error
- func Encode(ext string, v interface{}) ([]byte, error)
- func EncodeFile(file string, v interface{}) error
- func FalsePtr() *bool
- func Float64Ptr(f float64) *float64
- func GenerateRandString(n int) string
- func IntPtr(n int) *int
- func TruePtr() *bool
- func UniqStrSlice(ss []string) []string
- func WithStack(err error) error
- type Deletable
- type Loadable
- type Savable
- type StackTracer
- type TomlUndecodedKeysError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CalcBools ¶
CalcBools calculates an addition/subtraction result of b1 and b2. This method adds the two if add is true and otherwise subtracts.
Example ¶
package main import ( "github.com/iwataka/mybot/backend/utils" "fmt" ) func main() { fmt.Println(utils.CalcBools(true, true, true)) fmt.Println(utils.CalcBools(false, false, true)) fmt.Println(utils.CalcBools(true, true, false)) fmt.Println(utils.CalcBools(false, false, false)) }
Output: true false false false
func CalcStringSlices ¶
CalcStringSlices calculates an addition/subtraction result of s1 and s2. If add is true then this method adds the two and otherwise subtracts.
Example ¶
package main import ( "github.com/iwataka/mybot/backend/utils" "fmt" ) func main() { ss1 := []string{"foo", "bar"} ss2 := []string{"foo", "other"} fmt.Println(len(utils.CalcStringSlices(ss1, ss2, true))) fmt.Println(len(utils.CalcStringSlices(ss1, ss2, false))) }
Output: 3 1
func CheckStringContained ¶
CheckStringContained returns true if ss contains str and otherwise false.
Example ¶
package main import ( "github.com/iwataka/mybot/backend/utils" "fmt" ) func main() { ss := []string{"foo", "bar"} fmt.Println(utils.CheckStringContained(ss, "foo")) fmt.Println(utils.CheckStringContained(ss, "other")) }
Output: true false
func DecodeFile ¶
DecodeFile decodes file and write the content to v. This method selects a proper decoder by the file extension (json decoder by default).
func EncodeFile ¶
EncodeFile encodes v into file. This method selects a proper encoder by the file extension (json decoder by default).
func GenerateRandString ¶
GenerateRandString generates a random string consisted of n upper/lower-case alphabets.
func UniqStrSlice ¶
UniqStrSlice returns a slice contains only unique elements of ss. This keeps order of the original slice ss.
Types ¶
type Loadable ¶
type Loadable interface {
Load() error
}
Loadable provides a feature to load data from an outside storage and write it into this instance.
type Savable ¶
type Savable interface {
Save() error
}
Savable provides a feature to save the instance data into an outside storage.
type StackTracer ¶
type StackTracer interface {
StackTrace() errors.StackTrace
}
StackTracer provides a feature to get stack trace.
type TomlUndecodedKeysError ¶
TomlUndecodedKeysError is an error indicating that there are some undecoded keys in File. In other words, there are some keys which exist in File but not in a destination object.
Example ¶
package main import ( "github.com/BurntSushi/toml" "github.com/iwataka/mybot/backend/utils" "fmt" ) func main() { err := utils.TomlUndecodedKeysError{[]toml.Key{[]string{"foo"}}, "foo.toml"} fmt.Println(err.Error()) }
Output: [foo] undecoded in foo.toml
func (*TomlUndecodedKeysError) Error ¶
func (e *TomlUndecodedKeysError) Error() string
Error returns a message of this error.