utils

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArrayContains

func ArrayContains(haystack []string, needle string) (int, bool)

ArrayContains takes a slice of strings and looks for an element in it. If found it returns its index, otherwise it will return -1 and false.

Example
haystack := []string{"Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "India"}
needle := "Golf"
index, exists := ArrayContains(haystack, needle)

if !exists {
	fmt.Printf("Element %s could not be found", needle)
} else {
	fmt.Printf("Element %s was found with index %d", needle, index)
}
Output:

Element Golf was found with index 6

func CreateFile

func CreateFile(filepath string) error

CreateFile creates a file on the given filepath along with any necessary parents, and returns nil, or else returns an error.

Example
err := CreateFile(existingFile)
if err != nil {
	fmt.Println("Failed to create file because it already exists")
} else {
	fmt.Println("File has been created successfully!")
}
Output:

Failed to create file because it already exists

func FileExists

func FileExists(path string) bool

FileExists reports whether the provided file exists.

Example
exists := FileExists("/missing-file.txt")
if exists {
	fmt.Println("File exists")
} else {
	fmt.Println("File does not exist")
}
Output:

File does not exist

func FolderExists

func FolderExists(path string) bool

FolderExists reports whether the provided directory exists.

Example
exists := FolderExists("/a-non-existing-folder")
if exists {
	fmt.Println("Folder exists")
} else {
	fmt.Println("Folder does not exist")
}
Output:

Folder does not exist

func GetEnv

func GetEnv(key string) (string, error)

GetEnv returns the value of the environment variable specified by key, if the variable is set.

Example
variable := "MY_VAR"
os.Setenv(variable, "some-value")

val, err := GetEnv(variable)

if err == nil {
	fmt.Println(val)
} else {
	fmt.Println(err)
}
Output:

some-value

func IsOnline

func IsOnline(url url.URL) error

IsOnline checks the provided URL for connectivity

Example
google, _ := url.Parse("https://google.com")
if err := IsOnline(*google); err != nil {
	fmt.Println("Host is not accessible")
} else {
	fmt.Println("Host is accessible")
}
Output:

Host is accessible

func ParseAndGetDocument

func ParseAndGetDocument(uri string) *goquery.Document

ParseAndGetDocument fetches the GoQuery document object for a given link

func RemoveFile

func RemoveFile(path string) error

RemoveFile deletes the specified file

Example
_ = CreateFile(fileToDelete)
err := RemoveFile(fileToDelete)

if err == nil {
	fmt.Println("File deleted")
} else {
	fmt.Println("Unable to delete file")
}
Output:

File deleted

func StringContains

func StringContains(baseString string, stringlist ...string) bool

StringContains searches for a string in a series of strings

Example
contains := StringContains("This is a string", "not", "are", "simple")
if contains {
	fmt.Println("String contains at least one of the arguments")
} else {
	fmt.Println("String does not contain any of the arguments")
}
Output:

String does not contain any of the arguments

Types

This section is empty.

Jump to

Keyboard shortcuts

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