Files
gf/text/gstr/gstr_z_example_test.go

1359 lines
22 KiB
Go
Raw Permalink Normal View History

2021-11-18 22:13:12 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gstr_test
import (
"fmt"
"strconv"
"strings"
2021-11-18 22:13:12 +08:00
"github.com/gogf/gf/v2/text/gstr"
)
2021-12-14 23:54:15 +08:00
func ExampleCount() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `goframe is very, very easy to use`
substr1 = "goframe"
substr2 = "very"
result1 = gstr.Count(str, substr1)
result2 = gstr.Count(str, substr2)
2021-11-18 22:13:12 +08:00
)
2021-12-14 23:54:15 +08:00
fmt.Println(result1)
fmt.Println(result2)
2021-11-18 22:13:12 +08:00
// Output:
2021-12-14 23:54:15 +08:00
// 1
// 2
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleCountI() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `goframe is very, very easy to use`
substr1 = "GOFRAME"
substr2 = "VERY"
result1 = gstr.CountI(str, substr1)
result2 = gstr.CountI(str, substr2)
2021-11-18 22:13:12 +08:00
)
2021-12-14 23:54:15 +08:00
fmt.Println(result1)
fmt.Println(result2)
2021-11-18 22:13:12 +08:00
// Output:
2021-12-14 23:54:15 +08:00
// 1
// 2
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleToLower() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
s = `GOFRAME`
result = gstr.ToLower(s)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// goframe
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleToUpper() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
s = `goframe`
result = gstr.ToUpper(s)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// GOFRAME
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleUcFirst() {
var (
s = `hello`
result = gstr.UcFirst(s)
)
fmt.Println(result)
2021-11-18 22:13:12 +08:00
// Output:
2021-12-14 23:54:15 +08:00
// Hello
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleLcFirst() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `Goframe`
result = gstr.LcFirst(str)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// goframe
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleUcWords() {
2021-11-18 22:13:12 +08:00
var (
str = `hello world`
2021-12-14 23:54:15 +08:00
result = gstr.UcWords(str)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// Hello World
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleIsLetterLower() {
fmt.Println(gstr.IsLetterLower('a'))
fmt.Println(gstr.IsLetterLower('A'))
2021-11-18 22:13:12 +08:00
// Output:
2021-12-14 23:54:15 +08:00
// true
// false
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleIsLetterUpper() {
fmt.Println(gstr.IsLetterUpper('A'))
fmt.Println(gstr.IsLetterUpper('a'))
2021-11-18 22:13:12 +08:00
// Output:
2021-12-14 23:54:15 +08:00
// true
// false
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleIsNumeric() {
fmt.Println(gstr.IsNumeric("88"))
fmt.Println(gstr.IsNumeric("3.1415926"))
fmt.Println(gstr.IsNumeric("abc"))
// Output:
// true
// true
// false
}
func ExampleReverse() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `123456`
result = gstr.Reverse(str)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// 654321
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleNumberFormat() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
number float64 = 123456
decimals = 2
decPoint = "."
thousandsSep = ","
result = gstr.NumberFormat(number, decimals, decPoint, thousandsSep)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// 123,456.00
2021-11-18 22:13:12 +08:00
}
func ExampleChunkSplit() {
var (
body = `1234567890`
chunkLen = 2
end = "#"
result = gstr.ChunkSplit(body, chunkLen, end)
)
fmt.Println(result)
// Output:
// 12#34#56#78#90#
}
func ExampleCompare() {
fmt.Println(gstr.Compare("c", "c"))
fmt.Println(gstr.Compare("a", "b"))
fmt.Println(gstr.Compare("c", "b"))
// Output:
// 0
// -1
// 1
}
2021-12-14 23:54:15 +08:00
func ExampleEqual() {
fmt.Println(gstr.Equal(`A`, `a`))
fmt.Println(gstr.Equal(`A`, `A`))
fmt.Println(gstr.Equal(`A`, `B`))
2021-11-18 22:13:12 +08:00
// Output:
2021-12-14 23:54:15 +08:00
// true
// true
// false
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleFields() {
var (
str = `Hello World`
result = gstr.Fields(str)
)
fmt.Printf(`%#v`, result)
2021-11-18 22:13:12 +08:00
// Output:
2021-12-14 23:54:15 +08:00
// []string{"Hello", "World"}
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleHasPrefix() {
var (
s = `Hello World`
prefix = "Hello"
result = gstr.HasPrefix(s, prefix)
)
fmt.Println(result)
2021-11-18 22:13:12 +08:00
// Output:
// true
}
2021-12-14 23:54:15 +08:00
func ExampleHasSuffix() {
var (
s = `my best love is goframe`
prefix = "goframe"
result = gstr.HasSuffix(s, prefix)
)
fmt.Println(result)
// Output:
// true
}
func ExampleCountWords() {
var (
str = `goframe is very, very easy to use!`
result = gstr.CountWords(str)
)
fmt.Printf(`%#v`, result)
// Output:
// map[string]int{"easy":1, "goframe":1, "is":1, "to":1, "use!":1, "very":1, "very,":1}
}
func ExampleCountChars() {
var (
str = `goframe`
result = gstr.CountChars(str)
)
fmt.Println(result)
// May Output:
// map[a:1 e:1 f:1 g:1 m:1 o:1 r:1]
}
func ExampleWordWrap() {
2021-11-18 22:13:12 +08:00
{
var (
2021-12-14 23:54:15 +08:00
str = `A very long woooooooooooooooooord. and something`
width = 8
br = "\n"
result = gstr.WordWrap(str, width, br)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
}
{
var (
2021-12-14 23:54:15 +08:00
str = `The quick brown fox jumped over the lazy dog.`
width = 20
br = "<br />\n"
result = gstr.WordWrap(str, width, br)
2021-11-18 22:13:12 +08:00
)
2021-12-14 23:54:15 +08:00
fmt.Printf("%v", result)
2021-11-18 22:13:12 +08:00
}
// Output:
2021-12-14 23:54:15 +08:00
// A very
// long
// woooooooooooooooooord.
// and
// something
// The quick brown fox<br />
// jumped over the lazy<br />
// dog.
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleLenRune() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `GoFrame框架`
result = gstr.LenRune(str)
2021-11-18 22:13:12 +08:00
)
2021-12-14 23:54:15 +08:00
fmt.Println(result)
2021-11-18 22:13:12 +08:00
// Output:
2021-12-14 23:54:15 +08:00
// 9
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleRepeat() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
input = `goframe `
multiplier = 3
result = gstr.Repeat(input, multiplier)
2021-11-18 22:13:12 +08:00
)
2021-12-14 23:54:15 +08:00
fmt.Println(result)
2021-11-18 22:13:12 +08:00
// Output:
2021-12-14 23:54:15 +08:00
// goframe goframe goframe
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleShuffle() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `123456`
result = gstr.Shuffle(str)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// May Output:
2021-12-14 23:54:15 +08:00
// 563214
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleSplit() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `a|b|c|d`
delimiter = `|`
result = gstr.Split(str, delimiter)
2021-11-18 22:13:12 +08:00
)
2021-12-14 23:54:15 +08:00
fmt.Printf(`%#v`, result)
2021-11-18 22:13:12 +08:00
// Output:
2021-12-14 23:54:15 +08:00
// []string{"a", "b", "c", "d"}
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleSplitAndTrim() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `a|b|||||c|d`
delimiter = `|`
result = gstr.SplitAndTrim(str, delimiter)
2021-11-18 22:13:12 +08:00
)
fmt.Printf(`%#v`, result)
// Output:
2021-12-14 23:54:15 +08:00
// []string{"a", "b", "c", "d"}
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleJoin() {
var (
array = []string{"goframe", "is", "very", "easy", "to", "use"}
sep = ` `
result = gstr.Join(array, sep)
)
fmt.Println(result)
2021-11-18 22:13:12 +08:00
// Output:
2021-12-14 23:54:15 +08:00
// goframe is very easy to use
}
func ExampleJoinAny() {
var (
sep = `,`
arr2 = []int{99, 73, 85, 66}
result = gstr.JoinAny(arr2, sep)
)
fmt.Println(result)
// Output:
// 99,73,85,66
2021-11-18 22:13:12 +08:00
}
func ExampleExplode() {
var (
str = `Hello World`
delimiter = " "
result = gstr.Explode(delimiter, str)
)
fmt.Printf(`%#v`, result)
// Output:
// []string{"Hello", "World"}
}
2021-12-14 23:54:15 +08:00
func ExampleImplode() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
pieces = []string{"goframe", "is", "very", "easy", "to", "use"}
glue = " "
result = gstr.Implode(glue, pieces)
2021-11-18 22:13:12 +08:00
)
2021-12-14 23:54:15 +08:00
fmt.Println(result)
2021-11-18 22:13:12 +08:00
// Output:
2021-12-14 23:54:15 +08:00
// goframe is very easy to use
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleChr() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
ascii = 65 // A
result = gstr.Chr(ascii)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// A
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
// '103' is the 'g' in ASCII
func ExampleOrd() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `goframe`
result = gstr.Ord(str)
2021-11-18 22:13:12 +08:00
)
2021-12-14 23:54:15 +08:00
2021-11-18 22:13:12 +08:00
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// 103
2021-11-18 22:13:12 +08:00
}
func ExampleHideStr() {
var (
str = `13800138000`
percent = 40
hide = `*`
result = gstr.HideStr(str, percent, hide)
)
fmt.Println(result)
// Output:
// 138****8000
}
2021-12-14 23:54:15 +08:00
func ExampleNl2Br() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `goframe
is
very
easy
to
use`
result = gstr.Nl2Br(str)
2021-11-18 22:13:12 +08:00
)
2021-12-14 23:54:15 +08:00
2021-11-18 22:13:12 +08:00
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// goframe<br>is<br>very<br>easy<br>to<br>use
}
func ExampleAddSlashes() {
var (
str = `'aa'"bb"cc\r\n\d\t`
result = gstr.AddSlashes(str)
)
fmt.Println(result)
// Output:
// \'aa\'\"bb\"cc\\r\\n\\d\\t
}
func ExampleStripSlashes() {
var (
str = `C:\\windows\\GoFrame\\test`
result = gstr.StripSlashes(str)
)
fmt.Println(result)
// Output:
// C:\windows\GoFrame\test
}
func ExampleQuoteMeta() {
{
var (
str = `.\+?[^]()`
result = gstr.QuoteMeta(str)
)
fmt.Println(result)
}
{
var (
str = `https://goframe.org/pages/viewpage.action?pageId=1114327`
result = gstr.QuoteMeta(str)
)
fmt.Println(result)
}
// Output:
// \.\\\+\?\[\^\]\(\)
// https://goframe\.org/pages/viewpage\.action\?pageId=1114327
}
// array
func ExampleSearchArray() {
var (
array = []string{"goframe", "is", "very", "nice"}
str = `goframe`
result = gstr.SearchArray(array, str)
)
fmt.Println(result)
// Output:
// 0
2021-11-18 22:13:12 +08:00
}
func ExampleInArray() {
var (
a = []string{"goframe", "is", "very", "easy", "to", "use"}
s = "goframe"
result = gstr.InArray(a, s)
)
fmt.Println(result)
// Output:
// true
}
2021-12-14 23:54:15 +08:00
func ExamplePrefixArray() {
var (
strArray = []string{"tom", "lily", "john"}
)
gstr.PrefixArray(strArray, "classA_")
fmt.Println(strArray)
2021-11-18 22:13:12 +08:00
// Output:
// [classA_tom classA_lily classA_john]
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
// case
func ExampleCaseCamel() {
var (
str = `hello world`
result = gstr.CaseCamel(str)
)
fmt.Println(result)
2021-11-18 22:13:12 +08:00
// Output:
2021-12-14 23:54:15 +08:00
// HelloWorld
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleCaseCamelLower() {
var (
str = `hello world`
result = gstr.CaseCamelLower(str)
)
fmt.Println(result)
2021-11-18 22:13:12 +08:00
// Output:
2021-12-14 23:54:15 +08:00
// helloWorld
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleCaseSnake() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `hello world`
result = gstr.CaseSnake(str)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// hello_world
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleCaseSnakeScreaming() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `hello world`
result = gstr.CaseSnakeScreaming(str)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// HELLO_WORLD
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleCaseSnakeFirstUpper() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `RGBCodeMd5`
result = gstr.CaseSnakeFirstUpper(str)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// rgb_code_md5
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleCaseKebab() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `hello world`
result = gstr.CaseKebab(str)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// hello-world
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleCaseKebabScreaming() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `hello world`
result = gstr.CaseKebabScreaming(str)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// HELLO-WORLD
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleCaseDelimited() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `hello world`
del = byte('-')
result = gstr.CaseDelimited(str, del)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// hello-world
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleCaseDelimitedScreaming() {
{
var (
str = `hello world`
del = byte('-')
result = gstr.CaseDelimitedScreaming(str, del, true)
)
fmt.Println(result)
}
{
var (
str = `hello world`
del = byte('-')
result = gstr.CaseDelimitedScreaming(str, del, false)
)
fmt.Println(result)
}
2021-11-18 22:13:12 +08:00
2021-12-14 23:54:15 +08:00
// Output:
// HELLO-WORLD
// hello-world
}
// contain
func ExampleContains() {
{
var (
str = `Hello World`
substr = `Hello`
result = gstr.Contains(str, substr)
)
fmt.Println(result)
}
{
var (
str = `Hello World`
substr = `hello`
result = gstr.Contains(str, substr)
)
fmt.Println(result)
}
2021-11-18 22:13:12 +08:00
// Output:
2021-12-14 23:54:15 +08:00
// true
// false
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleContainsI() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `Hello World`
substr = "hello"
result1 = gstr.Contains(str, substr)
result2 = gstr.ContainsI(str, substr)
2021-11-18 22:13:12 +08:00
)
2021-12-14 23:54:15 +08:00
fmt.Println(result1)
fmt.Println(result2)
2021-11-18 22:13:12 +08:00
// Output:
2021-12-14 23:54:15 +08:00
// false
// true
}
func ExampleContainsAny() {
{
var (
s = `goframe`
chars = "g"
result = gstr.ContainsAny(s, chars)
)
fmt.Println(result)
}
{
var (
s = `goframe`
chars = "G"
result = gstr.ContainsAny(s, chars)
)
fmt.Println(result)
}
// Output:
// true
// false
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
// convert
2021-11-18 22:13:12 +08:00
func ExampleOctStr() {
var (
str = `\346\200\241`
result = gstr.OctStr(str)
)
fmt.Println(result)
// Output:
// 怡
}
2021-12-14 23:54:15 +08:00
// domain
func ExampleIsSubDomain() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
subDomain = `s.goframe.org`
mainDomain = `goframe.org`
result = gstr.IsSubDomain(subDomain, mainDomain)
2021-11-18 22:13:12 +08:00
)
2021-12-14 23:54:15 +08:00
fmt.Println(result)
// Output:
// true
}
2021-11-18 22:13:12 +08:00
2021-12-14 23:54:15 +08:00
// levenshtein
func ExampleLevenshtein() {
var (
str1 = "Hello World"
str2 = "hallo World"
costIns = 1
costRep = 1
costDel = 1
result = gstr.Levenshtein(str1, str2, costIns, costRep, costDel)
)
2021-11-18 22:13:12 +08:00
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// 2
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
// parse
2021-11-18 22:13:12 +08:00
func ExampleParse() {
{
var (
str = `v1=m&v2=n`
result, _ = gstr.Parse(str)
)
fmt.Println(result)
}
{
var (
str = `v[a][a]=m&v[a][b]=n`
result, _ = gstr.Parse(str)
)
fmt.Println(result)
}
{
// The form of nested Slice is not yet supported.
var str = `v[][]=m&v[][]=n`
result, err := gstr.Parse(str)
if err != nil {
panic(err)
}
fmt.Println(result)
}
{
// This will produce an error.
var str = `v=m&v[a]=n`
result, err := gstr.Parse(str)
if err != nil {
println(err)
}
fmt.Println(result)
}
{
var (
str = `a .[[b=c`
result, _ = gstr.Parse(str)
)
fmt.Println(result)
}
// May Output:
// map[v1:m v2:n]
// map[v:map[a:map[a:m b:n]]]
// map[v:map[]]
refactor: interface{} to any and reflect.Ptr to reflect.Pointer (#4395) This pull request standardizes the use of the Go 1.18+ `any` type alias instead of `interface{}` throughout the codebase. The change improves code readability and aligns with modern Go best practices. The update touches many files, including core data structures, code generation templates, logging utilities, and test data, ensuring consistency across all usages. **Type alias migration to `any`:** * Replaced all instances of `interface{}` with `any` in core data structures such as `garray` and in generated model structs (e.g., `TableUser`, `User1`, `User2`) to modernize type usage. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[3]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[4]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[5]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[6]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) * Updated function signatures, method parameters, and return types from `interface{}` to `any` in various parts of the codebase, including code generation, service logic, and logging utilities (e.g., `mlog`). [[1]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[2]](diffhunk://#diff-2b1953fb78cf3593d8c2c7d911e95b65fd0b847c30ed0b4d167d16fe6d781235L54-R74) [[3]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[4]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) [[5]](diffhunk://#diff-c5d51d56f487779a2b6207c7ad26c7a20bbadcc846ce094fe60ab4cabff58c51L107-R107) [[6]](diffhunk://#diff-f96e6a9fdb416eb1804ceaba1fe0ac637bff22c43837f8bb849c2366ce72d4a1L116-R121) [[7]](diffhunk://#diff-f94c83a1b08ae060d9346f4a6031fc4a7b9a0b894e02d9afaa09018b6598eac0L112-R112) [[8]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L36-R36) [[9]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L74-R74) [[10]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L96-R96) **Generated code and templates:** * Adjusted generated files and code generation templates to output `any` instead of `interface{}` for relevant struct fields and function signatures, ensuring that new code generation aligns with the updated convention. [[1]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[2]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[3]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[4]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[5]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) [[6]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[7]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[8]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) **Container and utility updates:** * Refactored the `garray` container implementation and related constructors/methods to use `[]any` instead of `[]interface{}`, along with corresponding function signatures. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L52-R52) [[3]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L62-R62) [[4]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L73-R86) [[5]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L96-R97) [[6]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L107-R114) [[7]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L124-R124) [[8]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L135-R143) [[9]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L167-R167) These changes collectively modernize the codebase and prepare it for future Go developments by using the idiomatic `any` type.
2025-08-28 16:53:19 +08:00
// Error: expected type 'map[string]any' for key 'v', but got 'string'
2021-11-18 22:13:12 +08:00
// map[]
// map[a___[b:c]
}
2021-12-14 23:54:15 +08:00
// pos
2021-11-18 22:13:12 +08:00
func ExamplePos() {
var (
haystack = `Hello World`
needle = `World`
result = gstr.Pos(haystack, needle)
)
fmt.Println(result)
// Output:
// 6
}
2021-12-14 23:54:15 +08:00
func ExamplePosRune() {
var (
haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
needle = `Go`
posI = gstr.PosRune(haystack, needle)
posR = gstr.PosRRune(haystack, needle)
)
fmt.Println(posI)
fmt.Println(posR)
// Output:
// 0
// 22
}
2021-11-18 22:13:12 +08:00
func ExamplePosI() {
var (
haystack = `goframe is very, very easy to use`
needle = `very`
posI = gstr.PosI(haystack, needle)
posR = gstr.PosR(haystack, needle)
)
fmt.Println(posI)
fmt.Println(posR)
// Output:
// 11
// 17
}
func ExamplePosIRune() {
{
var (
haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
needle = `高性能`
startOffset = 10
result = gstr.PosIRune(haystack, needle, startOffset)
)
fmt.Println(result)
}
{
var (
haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
needle = `高性能`
startOffset = 30
result = gstr.PosIRune(haystack, needle, startOffset)
)
fmt.Println(result)
}
// Output:
// 14
// -1
}
func ExamplePosR() {
var (
haystack = `goframe is very, very easy to use`
needle = `very`
posI = gstr.PosI(haystack, needle)
posR = gstr.PosR(haystack, needle)
)
fmt.Println(posI)
fmt.Println(posR)
// Output:
// 11
// 17
}
2021-12-14 23:54:15 +08:00
func ExamplePosRRune() {
2021-11-18 22:13:12 +08:00
var (
haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
2021-12-14 23:54:15 +08:00
needle = `Go`
2021-11-18 22:13:12 +08:00
posI = gstr.PosIRune(haystack, needle)
2021-12-14 23:54:15 +08:00
posR = gstr.PosRRune(haystack, needle)
2021-11-18 22:13:12 +08:00
)
fmt.Println(posI)
fmt.Println(posR)
// Output:
// 0
// 22
}
2021-12-14 23:54:15 +08:00
func ExamplePosRI() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
haystack = `goframe is very, very easy to use`
needle = `VERY`
posI = gstr.PosI(haystack, needle)
posR = gstr.PosRI(haystack, needle)
2021-11-18 22:13:12 +08:00
)
fmt.Println(posI)
fmt.Println(posR)
// Output:
2021-12-14 23:54:15 +08:00
// 11
// 17
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExamplePosRIRune() {
2021-11-18 22:13:12 +08:00
var (
haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
2021-12-14 23:54:15 +08:00
needle = `GO`
posI = gstr.PosIRune(haystack, needle)
posR = gstr.PosRIRune(haystack, needle)
2021-11-18 22:13:12 +08:00
)
fmt.Println(posI)
fmt.Println(posR)
// Output:
// 0
// 22
}
2021-12-14 23:54:15 +08:00
// replace
func ExampleReplace() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
origin = `golang is very nice!`
search = `golang`
replace = `goframe`
result = gstr.Replace(origin, search, replace)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// goframe is very nice!
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleReplaceI() {
2021-11-18 22:13:12 +08:00
var (
origin = `golang is very nice!`
2021-12-14 23:54:15 +08:00
search = `GOLANG`
2021-11-18 22:13:12 +08:00
replace = `goframe`
2021-12-14 23:54:15 +08:00
result = gstr.ReplaceI(origin, search, replace)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
// goframe is very nice!
}
func ExampleReplaceByArray() {
{
var (
origin = `golang is very nice`
array = []string{"lang", "frame"}
result = gstr.ReplaceByArray(origin, array)
)
fmt.Println(result)
}
{
var (
origin = `golang is very good`
array = []string{"golang", "goframe", "good", "nice"}
result = gstr.ReplaceByArray(origin, array)
)
fmt.Println(result)
}
// Output:
// goframe is very nice
// goframe is very nice
}
2021-12-14 23:54:15 +08:00
func ExampleReplaceIByArray() {
var (
origin = `golang is very Good`
array = []string{"Golang", "goframe", "GOOD", "nice"}
result = gstr.ReplaceIByArray(origin, array)
)
fmt.Println(result)
// Output:
// goframe is very nice
}
2021-11-18 22:13:12 +08:00
func ExampleReplaceByMap() {
{
var (
origin = `golang is very nice`
replaces = map[string]string{
"lang": "frame",
}
result = gstr.ReplaceByMap(origin, replaces)
)
fmt.Println(result)
}
{
var (
origin = `golang is very good`
replaces = map[string]string{
"golang": "goframe",
"good": "nice",
}
result = gstr.ReplaceByMap(origin, replaces)
)
fmt.Println(result)
}
// Output:
// goframe is very nice
// goframe is very nice
}
func ExampleReplaceIByMap() {
var (
origin = `golang is very nice`
replaces = map[string]string{
"Lang": "frame",
}
result = gstr.ReplaceIByMap(origin, replaces)
)
fmt.Println(result)
// Output:
// goframe is very nice
}
func ExampleReplaceFunc() {
str := "hello gf 2018~2020!"
// Replace "gf" with a custom function that returns "GoFrame"
result := gstr.ReplaceFunc(str, "gf", func(s string) string {
return "GoFrame"
})
fmt.Println(result)
// Replace numbers with their doubled values
result = gstr.ReplaceFunc("1 2 3", "2", func(s string) string {
n, _ := strconv.Atoi(s)
return strconv.Itoa(n * 2)
})
fmt.Println(result)
// Output:
// hello GoFrame 2018~2020!
// 1 4 3
}
func ExampleReplaceIFunc() {
str := "Hello GF, hello gf, HELLO Gf!"
// Replace any case variation of "gf" with "GoFrame"
result := gstr.ReplaceIFunc(str, "gf", func(s string) string {
return "GoFrame"
})
fmt.Println(result)
// Preserve the original case of each match
result = gstr.ReplaceIFunc(str, "gf", func(s string) string {
if s == strings.ToUpper(s) {
return "GOFRAME"
}
if s == strings.ToLower(s) {
return "goframe"
}
return "GoFrame"
})
fmt.Println(result)
// Output:
// Hello GoFrame, hello GoFrame, HELLO GoFrame!
// Hello GOFRAME, hello goframe, HELLO GoFrame!
}
2021-12-14 23:54:15 +08:00
// similartext
2021-11-18 22:13:12 +08:00
func ExampleSimilarText() {
var (
first = `AaBbCcDd`
second = `ad`
percent = 0.80
result = gstr.SimilarText(first, second, &percent)
)
fmt.Println(result)
// Output:
// 2
}
2021-12-14 23:54:15 +08:00
// soundex
2021-11-18 22:13:12 +08:00
func ExampleSoundex() {
var (
str1 = `Hello`
str2 = `Hallo`
result1 = gstr.Soundex(str1)
result2 = gstr.Soundex(str2)
)
fmt.Println(result1, result2)
// Output:
// H400 H400
}
2021-12-14 23:54:15 +08:00
// str
2021-11-18 22:13:12 +08:00
func ExampleStr() {
var (
haystack = `xxx.jpg`
needle = `.`
result = gstr.Str(haystack, needle)
)
fmt.Println(result)
// Output:
// .jpg
}
func ExampleStrEx() {
var (
haystack = `https://goframe.org/index.html?a=1&b=2`
needle = `?`
result = gstr.StrEx(haystack, needle)
)
fmt.Println(result)
// Output:
// a=1&b=2
}
func ExampleStrTill() {
var (
haystack = `https://goframe.org/index.html?test=123456`
needle = `?`
result = gstr.StrTill(haystack, needle)
)
fmt.Println(result)
// Output:
// https://goframe.org/index.html?
}
func ExampleStrTillEx() {
var (
haystack = `https://goframe.org/index.html?test=123456`
needle = `?`
result = gstr.StrTillEx(haystack, needle)
)
fmt.Println(result)
// Output:
// https://goframe.org/index.html
}
2021-12-14 23:54:15 +08:00
// substr
2021-11-18 22:13:12 +08:00
func ExampleSubStr() {
var (
str = `1234567890`
start = 0
length = 4
subStr = gstr.SubStr(str, start, length)
)
fmt.Println(subStr)
// Output:
// 1234
}
func ExampleSubStrRune() {
var (
str = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架。`
start = 14
length = 3
subStr = gstr.SubStrRune(str, start, length)
)
fmt.Println(subStr)
// Output:
// 高性能
}
2021-12-14 23:54:15 +08:00
func ExampleStrLimit() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `123456789`
length = 3
suffix = `...`
result = gstr.StrLimit(str, length, suffix)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// 123...
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleStrLimitRune() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架。`
length = 17
suffix = "..."
result = gstr.StrLimitRune(str, length, suffix)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// GoFrame是一款模块化、高性能...
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleSubStrFrom() {
var (
str = "我爱GoFrameGood"
need = ``
)
fmt.Println(gstr.SubStrFrom(str, need))
2021-12-14 23:54:15 +08:00
// Output:
// 爱GoFrameGood
2021-12-14 23:54:15 +08:00
}
func ExampleSubStrFromEx() {
var (
str = "我爱GoFrameGood"
need = ``
)
fmt.Println(gstr.SubStrFromEx(str, need))
2021-12-14 23:54:15 +08:00
// Output:
// GoFrameGood
2021-12-14 23:54:15 +08:00
}
func ExampleSubStrFromR() {
var (
str = "我爱GoFrameGood"
need = `Go`
)
fmt.Println(gstr.SubStrFromR(str, need))
2021-12-14 23:54:15 +08:00
// Output:
// Good
2021-12-14 23:54:15 +08:00
}
func ExampleSubStrFromREx() {
var (
str = "我爱GoFrameGood"
need = `Go`
)
fmt.Println(gstr.SubStrFromREx(str, need))
2021-12-14 23:54:15 +08:00
// Output:
// od
2021-12-14 23:54:15 +08:00
}
// trim
2021-11-18 22:13:12 +08:00
func ExampleTrim() {
var (
str = `*Hello World*`
characterMask = "*"
result = gstr.Trim(str, characterMask)
)
fmt.Println(result)
// Output:
// Hello World
}
2021-12-14 23:54:15 +08:00
func ExampleTrimStr() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `Hello World`
cut = "World"
count = -1
result = gstr.TrimStr(str, cut, count)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// Hello
2021-11-18 22:13:12 +08:00
}
func ExampleTrimLeft() {
var (
str = `*Hello World*`
characterMask = "*"
result = gstr.TrimLeft(str, characterMask)
)
fmt.Println(result)
// Output:
// Hello World*
}
func ExampleTrimLeftStr() {
var (
str = `**Hello World**`
cut = "*"
count = 1
result = gstr.TrimLeftStr(str, cut, count)
)
fmt.Println(result)
// Output:
// *Hello World**
}
func ExampleTrimRight() {
var (
str = `**Hello World**`
characterMask = "*def" // []byte{"*", "d", "e", "f"}
result = gstr.TrimRight(str, characterMask)
)
fmt.Println(result)
// Output:
// **Hello Worl
}
func ExampleTrimRightStr() {
var (
str = `Hello World!`
cut = "!"
count = -1
result = gstr.TrimRightStr(str, cut, count)
)
fmt.Println(result)
// Output:
// Hello World
}
2021-12-14 23:54:15 +08:00
func ExampleTrimAll() {
2021-11-18 22:13:12 +08:00
var (
2021-12-14 23:54:15 +08:00
str = `*Hello World*`
characterMask = "*"
result = gstr.TrimAll(str, characterMask)
2021-11-18 22:13:12 +08:00
)
fmt.Println(result)
// Output:
2021-12-14 23:54:15 +08:00
// HelloWorld
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
// version
func ExampleCompareVersion() {
fmt.Println(gstr.CompareVersion("v2.11.9", "v2.10.8"))
fmt.Println(gstr.CompareVersion("1.10.8", "1.19.7"))
fmt.Println(gstr.CompareVersion("2.8.beta", "2.8"))
2021-11-18 22:13:12 +08:00
// Output:
2021-12-14 23:54:15 +08:00
// 1
// -1
// 0
2021-11-18 22:13:12 +08:00
}
2021-12-14 23:54:15 +08:00
func ExampleCompareVersionGo() {
fmt.Println(gstr.CompareVersionGo("v2.11.9", "v2.10.8"))
fmt.Println(gstr.CompareVersionGo("v4.20.1", "v4.20.1+incompatible"))
fmt.Println(gstr.CompareVersionGo(
"v0.0.2-20180626092158-b2ccc119800e",
"v1.0.1-20190626092158-b2ccc519800e",
))
2021-11-18 22:13:12 +08:00
// Output:
2021-12-14 23:54:15 +08:00
// 1
// 1
// -1
2021-11-18 22:13:12 +08:00
}