oop

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Object-oriented abstract wrappers for basic types.

Index

Examples

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

func (self *List[T]) At(i int) (T, bool)
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

func (self *List[T]) Chunk(length int) []*List[T]
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

func (self *List[T]) Clone() *List[T]
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

func (self *List[T]) Concat(others ...*List[T]) *List[T]
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

func (self *List[T]) Contains(item T) bool
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

func (self *List[T]) Count(item T) int
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

func (self *List[T]) Diff(others ...*List[T]) *List[T]
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

func (self *List[T]) Equal(another *List[T]) bool
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

func (self *List[T]) Every(fn func(item T, idx int) bool) bool
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

func (self *List[T]) Filter(fn func(item T, idx int) bool) *List[T]
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

func (self *List[T]) Find(fn func(item T, idx int) bool) (T, bool)
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

func (self *List[T]) FindIndex(fn func(item T, idx int) bool) int
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

func (self *List[T]) FindLast(fn func(item T, idx int) bool) (T, bool)
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

func (self *List[T]) FindLastIndex(fn func(item T, idx int) bool) int
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

func (self *List[T]) ForEach(fn func(item T, idx int)) *List[T]
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]) GoString

func (self *List[T]) GoString() string

func (*List[T]) IndexOf

func (self *List[T]) IndexOf(item T) int
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

func (self *List[T]) Intersect(others ...*List[T]) *List[T]
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

func (self *List[T]) Join(sep string) string
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

func (self *List[T]) LastIndexOf(item T) int
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

func (self *List[T]) Length() int
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

func (self *List[T]) Push(items ...T) int
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

func (self *List[T]) Replace(start int, end int, values ...T) *List[T]
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

func (self *List[T]) Reverse() *List[T]
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

func (self *List[T]) Shuffle() *List[T]
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

func (self *List[T]) Slice(start int, end int) *List[T]
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

func (self *List[T]) Some(fn func(item T, idx int) bool) bool
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

func (self *List[T]) Sort() *List[T]
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]) String

func (self *List[T]) String() string

func (*List[T]) ToReversed

func (self *List[T]) ToReversed() *List[T]
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

func (self *List[T]) ToShuffled() *List[T]
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

func (self *List[T]) ToSorted() *List[T]
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

func (self *List[T]) Union(others ...*List[T]) *List[T]
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

func (self *List[T]) Uniq() *List[T]
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

func (self *List[T]) Unshift(items ...T) int
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

func (self *List[T]) Xor(others ...*List[T]) *List[T]
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

func (str String) At(i int) String
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

func (str String) Capitalize(all bool) String
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

func (str String) Chunk(length int) []String
Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/oop"
)

func main() {
	str := oop.String("你好世界")

	fmt.Println(str.Chunk(2))
}
Output:

[你好 世界]

func (String) Clone

func (str String) Clone() String
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

func (str String) Compare(another string) int
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

func (str String) Contains(sub string) bool
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

func (str String) Count(sub string) int
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

func (str String) EndsWith(sub string) bool
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

func (str String) Hyphenate() String
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

func (str String) IndexOf(sub string) int
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

func (str String) LastIndexOf(sub string) int
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

func (str String) Length() int
Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/oop"
)

func main() {
	str := oop.String("你好,世界!")

	fmt.Println(str.Length())
}
Output:

6

func (String) Match

func (str String) Match(pattern string) []String
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

func (str String) MatchAll(pattern string) [][]String
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

func (str String) PadEnd(finalLength int, padStr string) String
Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/oop"
)

func main() {
	str := oop.String("你好,世界!")

	fmt.Println(str.PadEnd(10, "*"))
}
Output:

你好,世界!****

func (String) PadStart

func (str String) PadStart(finalLength int, padStr string) String
Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/oop"
)

func main() {
	str := oop.String("你好,世界!")

	fmt.Println(str.PadStart(10, "*"))
}
Output:

****你好,世界!

func (String) Repeat

func (str String) Repeat(count int) String
Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/oop"
)

func main() {
	str := oop.String("你好,世界!")

	fmt.Println(str.Repeat(2))
}
Output:

你好,世界!你好,世界!

func (String) Replace

func (str String) Replace(old string, rep string) String
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

func (str String) ReplaceAll(old string, rep string) String
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

func (str String) Search(pattern string) int
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

func (str String) Slice(start int, end int) String
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

func (str String) Split(sep string) []String
Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/oop"
)

func main() {
	str := oop.String("你好,世界")

	fmt.Println(str.Split(","))
}
Output:

[你好 世界]

func (String) StartsWith

func (str String) StartsWith(sub string) bool
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) String

func (str String) String() string

func (String) Substring

func (str String) Substring(start int, end int) String
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

func (str String) ToLowerCase() String
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

func (str String) ToUpperCase() String
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

func (str String) Trim(chars string) String
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

func (str String) TrimLeft(chars string) String
Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/oop"
)

func main() {
	str := oop.String("  你好,世界!  ")

	fmt.Printf("%#v\n", str.TrimLeft(" "))
}
Output:

"你好,世界!  "

func (String) TrimRight

func (str String) TrimRight(chars string) String
Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/oop"
)

func main() {
	str := oop.String("  你好,世界!  ")

	fmt.Printf("%#v\n", str.TrimRight(" "))
}
Output:

"  你好,世界!"

func (String) Truncate

func (str String) Truncate(length int) String
Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/oop"
)

func main() {
	str := oop.String("你好,世界!Hallo 世界!")

	fmt.Println(str.Truncate(10))
}
Output:

你好,世界!H...

Jump to

Keyboard shortcuts

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