Documentation ¶
Overview ¶
Object-oriented abstract wrappers for basic types.
Index ¶
- type List
- func (self *List[T]) At(i int) (T, bool)
- func (self *List[T]) Chunk(length int) []*List[T]
- func (self *List[T]) Clone() *List[T]
- func (self *List[T]) Concat(others ...*List[T]) *List[T]
- func (self *List[T]) Contains(item T) bool
- func (self *List[T]) Count(item T) int
- func (self *List[T]) Diff(others ...*List[T]) *List[T]
- func (self *List[T]) Equal(another *List[T]) bool
- func (self *List[T]) Every(fn func(item T, idx int) bool) bool
- func (self *List[T]) Filter(fn func(item T, idx int) bool) *List[T]
- func (self *List[T]) Find(fn func(item T, idx int) bool) (T, bool)
- func (self *List[T]) FindIndex(fn func(item T, idx int) bool) int
- func (self *List[T]) FindLast(fn func(item T, idx int) bool) (T, bool)
- func (self *List[T]) FindLastIndex(fn func(item T, idx int) bool) int
- func (self *List[T]) ForEach(fn func(item T, idx int)) *List[T]
- func (self *List[T]) GoString() string
- func (self *List[T]) IndexOf(item T) int
- func (self *List[T]) Intersect(others ...*List[T]) *List[T]
- func (self *List[T]) Join(sep string) string
- func (self *List[T]) LastIndexOf(item T) int
- func (self *List[T]) Length() int
- func (self *List[T]) Pop() T
- func (self *List[T]) Push(items ...T) int
- func (self *List[T]) Replace(start int, end int, values ...T) *List[T]
- func (self *List[T]) Reverse() *List[T]
- func (self *List[T]) Shift() T
- func (self *List[T]) Shuffle() *List[T]
- func (self *List[T]) Slice(start int, end int) *List[T]
- func (self *List[T]) Some(fn func(item T, idx int) bool) bool
- func (self *List[T]) Sort() *List[T]
- func (self *List[T]) String() string
- func (self *List[T]) ToReversed() *List[T]
- func (self *List[T]) ToShuffled() *List[T]
- func (self *List[T]) ToSorted() *List[T]
- func (self *List[T]) Union(others ...*List[T]) *List[T]
- func (self *List[T]) Uniq() *List[T]
- func (self *List[T]) Unshift(items ...T) int
- func (self *List[T]) Values() []T
- func (self *List[T]) Xor(others ...*List[T]) *List[T]
- type String
- func (str String) At(i int) String
- func (str String) Capitalize(all bool) String
- func (str String) Chunk(length int) []String
- func (str String) Clone() String
- func (str String) Compare(another string) int
- func (str String) Contains(sub string) bool
- func (str String) Count(sub string) int
- func (str String) EndsWith(sub string) bool
- func (str String) Hyphenate() String
- func (str String) IndexOf(sub string) int
- func (str String) LastIndexOf(sub string) int
- func (str String) Length() int
- func (str String) Match(pattern string) []String
- func (str String) MatchAll(pattern string) [][]String
- func (str String) PadEnd(finalLength int, padStr string) String
- func (str String) PadStart(finalLength int, padStr string) String
- func (str String) Repeat(count int) String
- func (str String) Replace(old string, rep string) String
- func (str String) ReplaceAll(old string, rep string) String
- func (str String) Search(pattern string) int
- func (str String) Slice(start int, end int) String
- func (str String) Split(sep string) []String
- func (str String) StartsWith(sub string) bool
- func (str String) String() string
- func (str String) Substring(start int, end int) String
- func (str String) ToLowerCase() String
- func (str String) ToUpperCase() String
- func (str String) Trim(chars string) String
- func (str String) TrimLeft(chars string) String
- func (str String) TrimRight(chars string) String
- func (str String) Truncate(length int) String
Examples ¶
- List
- List (Json)
- List.At
- List.Chunk
- List.Clone
- List.Concat
- List.Contains
- List.Count
- List.Diff
- List.Equal
- List.Every
- List.Filter
- List.Find
- List.FindIndex
- List.FindLast
- List.FindLastIndex
- List.ForEach
- List.IndexOf
- List.Intersect
- List.Join
- List.LastIndexOf
- List.Length
- List.Pop
- List.Push
- List.Replace
- List.Reverse
- List.Shift
- List.Shuffle
- List.Slice
- List.Some
- List.Sort
- List.ToReversed
- List.ToShuffled
- List.ToSorted
- List.Union
- List.Uniq
- List.Unshift
- List.Values
- List.Xor
- NewList
- String
- String.At
- String.Capitalize
- String.Chunk
- String.Clone
- String.Compare
- String.Contains
- String.Count
- String.EndsWith
- String.Hyphenate
- String.IndexOf
- String.LastIndexOf
- String.Length
- String.Match
- String.MatchAll
- String.PadEnd
- String.PadStart
- String.Repeat
- String.Replace
- String.ReplaceAll
- String.Search
- String.Slice
- String.Split
- String.StartsWith
- String.Substring
- String.ToLowerCase
- String.ToUpperCase
- String.Trim
- String.TrimLeft
- String.TrimRight
- String.Truncate
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type List ¶
type List[T comparable] []T
List is an objected-oriented abstract that works around the slice and acts as a dynamic array.
Sadly, due to the limitation of Golang's generics, methods like `Map()`, `Reduce()` and `GroupBy()` cannot be implemented in List, we'll have to use them from the `slicex` package.
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := &oop.List[string]{"foo", "bar"} // use & for literal creation fmt.Println(list) fmt.Printf("%#v\n", list) }
Output: &[foo bar] &oop.List[string]{"foo", "bar"}
Example (Json) ¶
package main import ( "encoding/json" "fmt" "log" "github.com/ayonli/goext/oop" ) func main() { jsonStr := `["foo","bar"]` list := &oop.List[string]{} err := json.Unmarshal([]byte(jsonStr), list) if err != nil { log.Fatal(err) } fmt.Printf("%#v", list) }
Output: &oop.List[string]{"foo", "bar"}
func NewList ¶
func NewList[T comparable](base []T) *List[T]
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar"}) fmt.Println(list) fmt.Printf("%#v\n", list) }
Output: &[foo bar] &oop.List[string]{"foo", "bar"}
func (*List[T]) At ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar"}) fmt.Println(list.At(0)) fmt.Println(list.At(-1)) // negative indexing is supported fmt.Println(list.At(2)) // exceeding boundary returns zero-value and `false` }
Output: foo true bar true false
func (*List[T]) Chunk ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar", "hello", "world"}) chunks := list.Chunk(2) fmt.Println(chunks) fmt.Printf("%#v\n", chunks) }
Output: [&[foo bar] &[hello world]] []*oop.List[string]{&oop.List[string]{"foo", "bar"}, &oop.List[string]{"hello", "world"}}
func (*List[T]) Clone ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list1 := oop.NewList([]string{"foo", "bar"}) list2 := list1.Clone() fmt.Println(list2) }
Output: &[foo bar]
func (*List[T]) Concat ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list1 := oop.NewList([]string{"foo", "bar"}) list2 := list1.Concat(oop.NewList([]string{"Hello", "World"})) fmt.Println(list2) }
Output: &[foo bar Hello World]
func (*List[T]) Contains ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar"}) fmt.Println(list.Contains("foo")) fmt.Println(list.Contains("Hello")) }
Output: true false
func (*List[T]) Count ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar", "foo"}) fmt.Println(list.Count("foo")) fmt.Println(list.Count("bar")) fmt.Println(list.Count("Hello")) }
Output: 2 1 0
func (*List[T]) Diff ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list1 := oop.NewList([]int{0, 1, 2, 3, 4, 5}) list2 := oop.NewList([]int{2, 3, 4, 5, 6, 7}) list3 := list1.Diff(list2) list4 := list1.Diff(list2, nil) // nil will be ignored fmt.Println(list3) fmt.Println(list4) }
Output: &[0 1] &[0 1]
func (*List[T]) Equal ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list1 := oop.NewList([]string{"foo", "bar"}) list2 := oop.NewList([]string{"foo", "bar"}) list3 := oop.NewList([]string{"Hello", "World"}) fmt.Println(list1.Equal(list2)) fmt.Println(list1.Equal(list3)) }
Output: true false
func (*List[T]) Every ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list1 := oop.NewList([]string{"foo", "bar"}) ok1 := list1.Every(func(item string, idx int) bool { return len(item) > 0 }) ok2 := list1.Every(func(item string, idx int) bool { return item[0:1] == "f" }) fmt.Println(ok1) fmt.Println(ok2) }
Output: true false
func (*List[T]) Filter ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list1 := oop.NewList([]string{"foo", "bar", "hello", "world"}) list2 := list1.Filter(func(item string, idx int) bool { return len(item) > 3 }) fmt.Println(list2) }
Output: &[hello world]
func (*List[T]) Find ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar"}) item1, ok1 := list.Find(func(item string, idx int) bool { return item[0:1] == "f" }) item2, ok2 := list.Find(func(item string, idx int) bool { return item[0:1] == "a" }) fmt.Println(item1, ok1) fmt.Println(item2, ok2) }
Output: foo true false
func (*List[T]) FindIndex ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar"}) idx1 := list.FindIndex(func(item string, idx int) bool { return item[0:1] == "f" }) idx2 := list.FindIndex(func(item string, idx int) bool { return item[0:1] == "a" }) fmt.Println(idx1) fmt.Println(idx2) }
Output: 0 -1
func (*List[T]) FindLast ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar"}) item1, ok1 := list.FindLast(func(item string, idx int) bool { return len(item) > 0 }) item2, ok2 := list.FindLast(func(item string, idx int) bool { return len(item) > 3 }) fmt.Println(item1, ok1) fmt.Println(item2, ok2) }
Output: bar true false
func (*List[T]) FindLastIndex ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar"}) idx1 := list.FindLastIndex(func(item string, idx int) bool { return item[0:1] == "b" }) idx2 := list.FindLastIndex(func(item string, idx int) bool { return item[0:1] == "a" }) fmt.Println(idx1) fmt.Println(idx2) }
Output: 1 -1
func (*List[T]) ForEach ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar"}) list.ForEach(func(item string, idx int) { fmt.Println(idx, "=>", item) }) }
Output: 0 => foo 1 => bar
func (*List[T]) IndexOf ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar"}) fmt.Println(list.IndexOf("foo")) fmt.Println(list.IndexOf("Hello")) }
Output: 0 -1
func (*List[T]) Intersect ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list1 := oop.NewList([]int{0, 1, 2, 3, 4, 5}) list2 := oop.NewList([]int{2, 3, 4, 5, 6, 7}) list3 := list1.Intersect(list2) fmt.Println(list3) }
Output: &[2 3 4 5]
func (*List[T]) Join ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar"}) fmt.Println(list.Join(",")) }
Output: foo,bar
func (*List[T]) LastIndexOf ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar", "foo", "bar"}) fmt.Println(list.LastIndexOf("foo")) fmt.Println(list.LastIndexOf("Hello")) }
Output: 2 -1
func (*List[T]) Length ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar"}) fmt.Println(list.Length()) }
Output: 2
func (*List[T]) Pop ¶
func (self *List[T]) Pop() T
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar"}) item := list.Pop() fmt.Println(item) fmt.Println(list) }
Output: bar &[foo]
func (*List[T]) Push ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar"}) length := list.Push("hello", "world") fmt.Println(length) fmt.Println(list) }
Output: 4 &[foo bar hello world]
func (*List[T]) Replace ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar", "hello", "world"}) list.Replace(2, 4, "hi", "ayon") // Replace() mutates the list and returns itself fmt.Println(list) }
Output: &[foo bar hi ayon]
func (*List[T]) Reverse ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar"}) list.Reverse() // Reverse() mutates the list and returns itself fmt.Println(list) }
Output: &[bar foo]
func (*List[T]) Shift ¶
func (self *List[T]) Shift() T
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar"}) item := list.Shift() fmt.Println(item) fmt.Println(list) }
Output: foo &[bar]
func (*List[T]) Shuffle ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list1 := oop.NewList([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}) list2 := list1.Clone() list3 := list1.Clone() list2.Shuffle() // Shuffle() mutates the list and returns itself list3.Shuffle() fmt.Printf("list2(len: %d) != list1(len: %d): %v\n", list2.Length(), list1.Length(), !list2.Equal(list1)) fmt.Printf("list3(len: %d) != list1(len: %d): %v\n", list3.Length(), list1.Length(), !list3.Equal(list1)) fmt.Printf("list3(len: %d) != list2(len: %d): %v\n", list3.Length(), list2.Length(), !list3.Equal(list2)) }
Output: list2(len: 10) != list1(len: 10): true list3(len: 10) != list1(len: 10): true list3(len: 10) != list2(len: 10): true
func (*List[T]) Slice ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar", "hello", "world"}) fmt.Println(list.Slice(0, 2)) fmt.Println(list.Slice(-2, list.Length())) // negative indexing is supported }
Output: &[foo bar] &[hello world]
func (*List[T]) Some ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list1 := oop.NewList([]string{"foo", "bar"}) ok1 := list1.Some(func(item string, idx int) bool { return item[0:1] == "f" }) ok2 := list1.Some(func(item string, idx int) bool { return len(item) > 3 }) fmt.Println(ok1) fmt.Println(ok2) }
Output: true false
func (*List[T]) Sort ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar"}) list.Sort() // Sort() mutates the list and returns itself fmt.Println(list) }
Output: &[bar foo]
func (*List[T]) ToReversed ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list1 := oop.NewList([]string{"foo", "bar"}) list2 := list1.ToReversed() // ToReversed() returns a copy of the list with all items reversed fmt.Println(list1) fmt.Println(list2) }
Output: &[foo bar] &[bar foo]
func (*List[T]) ToShuffled ¶ added in v0.3.5
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list1 := oop.NewList([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}) list2 := list1.ToShuffled() fmt.Println(list1) // list1 is not modified fmt.Printf("list2(len: %d) != list1(len: %d): %v\n", list2.Length(), list1.Length(), !list2.Equal(list1)) }
Output: &[0 1 2 3 4 5 6 7 8 9] list2(len: 10) != list1(len: 10): true
func (*List[T]) ToSorted ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list1 := oop.NewList([]string{"foo", "bar"}) list2 := list1.ToSorted() // ToSorted() returns a copy of the list with all items reversed fmt.Println(list1) fmt.Println(list2) }
Output: &[foo bar] &[bar foo]
func (*List[T]) Union ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list1 := oop.NewList([]int{0, 1, 2, 3, 4, 5}) list2 := oop.NewList([]int{2, 3, 4, 5, 6, 7}) list3 := list1.Union(list2) list4 := list1.Union(list2, nil) // nil will be ignored fmt.Println(list3) fmt.Println(list4) }
Output: &[0 1 2 3 4 5 6 7] &[0 1 2 3 4 5 6 7]
func (*List[T]) Uniq ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"hello", "world", "hi", "world"}) fmt.Println(list.Uniq()) }
Output: &[hello world hi]
func (*List[T]) Unshift ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar"}) length := list.Unshift("hello", "world") fmt.Println(length) fmt.Println(list) }
Output: 4 &[hello world foo bar]
func (*List[T]) Values ¶
func (self *List[T]) Values() []T
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list := oop.NewList([]string{"foo", "bar"}) fmt.Println(list.Values()) }
Output: [foo bar]
func (*List[T]) Xor ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { list1 := oop.NewList([]int{0, 1, 2, 3, 4, 5}) list2 := oop.NewList([]int{2, 3, 4, 5, 6, 7}) list3 := list1.Xor(list2) list4 := list1.Xor(list2, nil) // nil will be ignored fmt.Println(list3) fmt.Println(list4) }
Output: &[0 1 6 7] &[0 1 6 7]
type String ¶
type String string
String is an object-oriented abstract that works around multi-byte strings.
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界!") fmt.Println(str) fmt.Printf("%#v", str) }
Output: 你好,世界! "你好,世界!"
func (String) At ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界!") fmt.Println(str.At(0)) // do not use str[0] or str[0:1] as they refer to a single byte fmt.Println(str.At(1)) // do not use str[1] or str[1:2] as they refer to a single byte fmt.Println(str.At(-1)) // negative index counts backwards }
Output: 你 好 !
func (String) Capitalize ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("hello, world!") fmt.Println(str.Capitalize(false)) fmt.Println(str.Capitalize(true)) }
Output: Hello, world! Hello, World!
func (String) Chunk ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好世界") fmt.Println(str.Chunk(2)) }
Output: [你好 世界]
func (String) Clone ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str1 := oop.String("你好,世界!") str2 := str1.Clone() fmt.Println(str2) }
Output: 你好,世界!
func (String) Compare ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str1 := oop.String("你好,世界!") fmt.Println(str1.Compare("你好,世界!")) fmt.Println(str1.Compare("Hello,世界!")) fmt.Println(str1.Compare("你好,祖国!")) }
Output: 0 1 -1
func (String) Contains ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界!") fmt.Println(str.Contains("你好")) fmt.Println(str.Contains("Hello")) }
Output: true false
func (String) Count ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界!") fmt.Println(str.Count("你好")) fmt.Println(str.Count("Hello")) }
Output: 1 0
func (String) EndsWith ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界!") fmt.Println(str.EndsWith("世界!")) fmt.Println(str.EndsWith("World!")) }
Output: true false
func (String) Hyphenate ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("hello world") fmt.Println(str.Hyphenate()) }
Output: hello-world
func (String) IndexOf ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界!") fmt.Println(str.IndexOf("世界")) fmt.Println(str.IndexOf("哈啰")) }
Output: 3 -1
func (String) LastIndexOf ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界!嗨,世界!") fmt.Println(str.LastIndexOf("世界")) fmt.Println(str.LastIndexOf("哈啰")) }
Output: 8 -1
func (String) Length ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界!") fmt.Println(str.Length()) }
Output: 6
func (String) Match ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界!Hello, 世界!") fmt.Println(str.Match("l{2,}")) }
Output: [ll]
func (String) MatchAll ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("Hello,World!Hi, 世界!") fmt.Println(str.MatchAll("(?i)h{1,}")) }
Output: [[H] [H]]
func (String) PadEnd ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界!") fmt.Println(str.PadEnd(10, "*")) }
Output: 你好,世界!****
func (String) PadStart ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界!") fmt.Println(str.PadStart(10, "*")) }
Output: ****你好,世界!
func (String) Repeat ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界!") fmt.Println(str.Repeat(2)) }
Output: 你好,世界!你好,世界!
func (String) Replace ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界!") fmt.Println(str.Replace("你好", "Hello")) }
Output: Hello,世界!
func (String) ReplaceAll ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界!Hello, 世界!") fmt.Println(str.ReplaceAll("世界", "World")) }
Output: 你好,World!Hello, World!
func (String) Search ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界!Hello, 世界!") fmt.Println(str.Search("l{2,}")) }
Output: 8
func (String) Slice ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界!") fmt.Println(str.Slice(0, 2)) // don't use str[0:2] as it refers to only 2 bytes fmt.Println(str.Slice(-3, -1)) // negative index counts backwards }
Output: 你好 世界
func (String) Split ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界") fmt.Println(str.Split(",")) }
Output: [你好 世界]
func (String) StartsWith ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界!") fmt.Println(str.StartsWith("你好")) fmt.Println(str.StartsWith("Hello")) }
Output: true false
func (String) Substring ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("你好,世界!") fmt.Println(str.Substring(0, 2)) // don't use str[0:2] as it refers to only 2 bytes fmt.Println(str.Substring(-3, -1)) // negative index are not supported, returns an empty string }
Output: 你好
func (String) ToLowerCase ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("Hello, World!") fmt.Println(str.ToLowerCase()) }
Output: hello, world!
func (String) ToUpperCase ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String("Hello, World!") fmt.Println(str.ToUpperCase()) }
Output: HELLO, WORLD!
func (String) Trim ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String(" 你好,世界! ") fmt.Printf("%#v\n", str.Trim(" ")) }
Output: "你好,世界!"
func (String) TrimLeft ¶
Example ¶
package main import ( "fmt" "github.com/ayonli/goext/oop" ) func main() { str := oop.String(" 你好,世界! ") fmt.Printf("%#v\n", str.TrimLeft(" ")) }
Output: "你好,世界! "
Click to show internal directories.
Click to hide internal directories.