mytoolkits

package module
v0.0.0-...-5b27b87 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2021 License: Apache-2.0 Imports: 4 Imported by: 1

README

The package mytoolkits provides a bunch of functions that I ususally use in my programming. For example some functions fo reading/saving strings from/to files, remove duplicates from a slice of strings, or find a specific pattern in strings and these type of functions....

Documentation

Overview

The package mytoolkits provides a bunch of functions that I ususally use in my programming

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindBetweenString

func FindBetweenString(s, start, end string) ([]int, string)

FindBetweenString gets three strings 's', 'start' and 'end' as parameters then search 's' to find a string begins with 'start' and after that continue searching to find a string begins with 'end' and returns the index of what is found If nothing is found {-1,-1} is returned

Example
// Assume that we have a text like below (note this is a string not a slice of string)
strdata := `aaaaaaaaa bbbbbbb ccccccc index1 dddddd eeeeee ffffff ggggg index2 
	hhhhhh iiiiii jjjjjj `

iresult, str := FindBetweenString(strdata, " index1 ", " index2 ")

if iresult[0] != -1 && iresult[1] != -1 {
	fmt.Println("index1 is in: ", iresult[0])
	fmt.Println("index2 is in: ", iresult[1])
	fmt.Println("The string is: ", str)
} else {
	fmt.Println("Nothing is found")
}
Output:

func FindPatternSliceString

func FindPatternSliceString(s, start, end string) []string

FindPatternSliceString uses the function FindPatternString to find all strings with a specific paterns in a string 's'. The patern should begin with 'start' and finish with 'end' while 'start' and 'end' are the function parameter. Note 'start' ≠ 'end' If nothing is found, nil is returned

Example
// To understand this example, you need to check the example of the function FindPatternString
// Assume that we have a text like below (note this is a string not a slice of string)
// We ou need to find all strings with has specific format or pattern. The patterns starts with specific string
// and ends with another string. For example, here we are trying to find:
//  <response status="success"><result>DATA1</result></response>
//	<response status="success"><result>DATA2</result></response>
//	<response status="success"><result>DATA3</result></response>
//	<response status="success"><result>DATA4</result></response>
// in the given text. Our strings all start with "<response status="success"><result>"
// and end with "</result></response>"
strdata := `garbage string like blah blah not important text
		<response status="success"><result>DATA1</result></response>
		garbage string like blah blah not important text
		<response status="success"><result>DATA2</result></response>
		<response status="success"><result>DATA3</result></response>
		garbage string like blah blah not important text
		garbage string like blah blah not important text
		<response status="success"><result>DATA4</result></response>
		garbage string like blah blah not important text`

// the function FindPatternSliceString gets three parameters: FindPatternString(s, start, end string). In this example:
// s will be strdata
// start will be <response status="success"><result>
// end will be </result></response>
strSlice := FindPatternSliceString(strdata, `<response status="success"><result>`, `</result></response>`)

for _, item := range strSlice {
	fmt.Println(item)
}
Output:

<response status="success"><result>DATA1</result></response>
<response status="success"><result>DATA2</result></response>
<response status="success"><result>DATA3</result></response>
<response status="success"><result>DATA4</result></response>

func FindPatternString

func FindPatternString(s, start, end string) ([]int, string)

FindPatternString finds a specific patterns in a text. To do this, it gets a string 's' and 'start', 'end' as two other string as prapeters and search in the entier 's' to find a substring and begins with 'start' and finishes with 'end'. Note 'start' ≠ 'end' If nothing is found {-1,-1} is returned

Example
// Assume that we have a text like below (note this is a string not a slice of string)
// We ou need to find a string with has specific format or pattern. The patterns starts with specific string
// and ends with another string. For example, here we are trying to find:
// <response status="success"><result>DATA1</result></response>
// in the given text. Our string starts with "<response status="success"><result>"
// and ends with "</result></response>"
strdata := `garbage string like blah blah not important
		<response status="success"><result>DATA1</result></response>
		garbage string like blah blah not important`

// the function FindPatternString gets three parameters: FindPatternString(s, start, end string). In this example:
// s will be strdata
// start will be <response status="success"><result>
// end will be </result></response>
var strSlice []string
var idx int
l := len(strdata)
for i := 0; i < l; i++ {
	iresult, sresult := FindPatternString(strdata[idx:], `<response status="success"><result>`, `</result></response>`)
	if iresult[0] != -1 && iresult[1] != -1 {
		idx = idx + iresult[1]
		strSlice = append(strSlice, sresult)
	} else {
		break
	}
}

fmt.Println(strSlice)
Output:

<response status="success"><result>DATA1</result></response>

func ReadTextFileLoadToMem

func ReadTextFileLoadToMem(fname string) (string, []string)

ReadTextFileLoadToMem gets filepath (path+filename), reads it and returns it as a string as well as a slice of string

Example
// assume that the file myfile.txt in in the current directory
// and the content of the file is aaaaaa bbbbbb cccccc
// Read the file and returns a string and a slice of string
str, sliceStr := ReadTextFileLoadToMem("./myfile.txt")
fmt.Println(str)
fmt.Println(sliceStr)
Output:

aaaaaa bbbbbb cccccc
[aaaaaa bbbbbb cccccc]

func RemoveDuplicatesFromSliceString

func RemoveDuplicatesFromSliceString(sliceStr []string) []string

RemoveDuplicatesFromSliceString gets a slice of string and remove duplicate string from it

Example
// Create a slice of string
strSlice := []string{
	"aaaaaa",
	"bbbbbb",
	"cccccc",
	"aaaaaa",
	"dddddd",
	"cccccc",
}

//Remove all duplicates in the slice strSlice and save the result in tmpSlice
tmpSlice := RemoveDuplicatesFromSliceString(strSlice)
fmt.Println(tmpSlice)
Output:

[aaaaaa bbbbbb cccccc dddddd]

func SaveSliceStrToFile

func SaveSliceStrToFile(fname string, value []string)

SaveSliceStrToFile gets fname (path+filename) and value as slice of string that should be stored in the file

Example
// Create a slice of string
strSlice := []string{
	"aaaaaa",
	"bbbbbb",
	"cccccc",
	"aaaaaa",
	"dddddd",
	"cccccc",
}

// Save the slice of string  to the file myfile.txt as a string
SaveSliceStrToFile("./myfile.txt", strSlice)
Output:

func SaveStringToFile

func SaveStringToFile(fname, values string)

SaveStringToFile gets fname (path+filename) and values that should be stored in the file

Example
// Create a string of data
str := "aaaaaa bbbbbb cccccc"

// Save the string str to the file myfile.txt as a string
SaveStringToFile("./myfile.txt", str)
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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