I have created a pretty bad (just being honest) Golang library. I will take any and all advice, as long as it's helpful. Sorry it's so long. Also, I easily forget some things, so many functions in convert
are just light wrappers around strconv
, etc.
Convert
package convert
import (
"github.com/retep-mathwizard/utils/depend"
o "github.com/retep-mathwizard/utils/other"
"strconv"
"strings"
"math"
)
func IntToString(num int) string {
str := strconv.Itoa(num)
return str
}
func StringToInt(str string) int {
num, err := strconv.Atoi(str)
if err != nil {
o.Exit("there was an error converting the type")
}
return num
}
func FloatToString(dec float64) string {
str := strconv.FormatFloat(dec, 'f', -1, 64)
return str
}
func StringToFloat(str string) float64 {
dec, err := strconv.ParseFloat(str, 64)
if err != nil {
o.Exit("there was an error converting the type")
}
return dec
}
func FloatToInt(dec float64) int {
num := int(dec)
return num
}
func IntToFloat(num int) float64 {
dec := float64(num)
return dec
}
func BoolToString(boo bool) string {
str := strconv.FormatBool(boo)
return str
}
func StringToBool(str string) bool {
boo, err := strconv.ParseBool(str)
if err != nil {
o.Exit("there was an error converting the type")
}
return boo
}
func StringToSlice(str string, splitat string) []string {
stringSlice := strings.Split(str, splitat)
return stringSlice
}
func SliceToString(array []string) string {
str := strings.Join(array, "")
return str
}
func IntToArray(num int) []int {
list := []int{}
digits := int(math.Log10(float64(num))) + 1
for i := digits - 1; i >= 0; i-- {
list = append(list, depend.IndexInt(num, i))
}
return list
}
func IntArrayToStringArray(list []int) []string {
newlist := []string{}
for _, item := range list {
stritem := IntToString(item)
newlist = append(newlist, stritem)
}
return newlist
}
func StringArrayToIntArray(list []string) []int {
newlist := []int{}
for _, item := range list {
intitem := StringToInt(item)
newlist = append(newlist, intitem)
}
return newlist
}
Input
package input
import (
"bufio"
"fmt"
c "github.com/skilstak/go/colors"
"os"
"strconv"
"strings"
)
//credit goes to @whitman
func IntInput(p string) int {
//This function just returns whatever is typed as a integer.
fmt.Print(c.X + p)
reader := bufio.NewReader(os.Stdin)
t, err := reader.ReadString('\n')
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
t = strings.TrimSpace(t)
i, err := strconv.Atoi(t)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
return i
}
//credit goes to @whitman
func StringInput(messageToUser string) string {
//This function just returns whatever is typed as a string.
fmt.Print(c.X + messageToUser)
reader := bufio.NewReader(os.Stdin)
t, err := reader.ReadString('\n')
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
t = strings.TrimSpace(t)
t = strings.ToLower(t)
//takes off whitespace and makes it lowercase
return t
}
//credit goes to @whitman
func FloatInput(messageToUser string) float64 {
//This function just returns whatever is typed as float64, a decimal or integer.
fmt.Print(c.X + messageToUser)
reader := bufio.NewReader(os.Stdin)
t, err := reader.ReadString('\n')
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
t = strings.TrimSpace(t)
f, err := strconv.ParseFloat(t, 64)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
return f
}
mmath
package mmath
import (
"math"
"math/rand"
"time"
)
func Round(f float64) int {
return int(math.Floor(f + .5))
}
func RandInt(low int, high int) int {
rand.Seed(time.Now().UnixNano())
randRoll := rand.Intn(high-low) + low
return randRoll
}
func SumIntSlice(intslice []int) (sum int) {
sum := 0
for _, v := range intslice {
sum += v
}
return sum
}
func SumFloat64Slice(intslice []float64) (sum float64) {
var sum float64 = 0.0
for _, v := range intslice {
sum += v
}
return sum
}
mod
package mod
import (
"math/rand"
"time"
)
func RandStringItem(list []string) string {
rand.Seed(time.Now().UnixNano())
randNum := rand.Intn(len(list))
randElement := list[randNum]
return randElement
}
func RandIntItem(list []int) int {
rand.Seed(time.Now().UnixNano())
randNum := rand.Intn(len(list))
randElement := list[randNum]
return randElement
}
func ReverseStringSlice(list []string) []string {
for i, j := 0, len(list)-1; i < j; i, j = i+1, j-1 {
list[i], list[j] = list[j], list[i]
}
return list
}
func ReverseIntSlice(list []int) []int {
for i, j := 0, len(list)-1; i < j; i, j = i+1, j-1 {
list[i], list[j] = list[j], list[i]
}
return list
}
func ReverseString(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}
func ReverseInt(n int) int {
new_int := 0
for n > 0 {
remainder := n % 10
new_int *= 10
new_int += remainder
n /= 10
}
return new_int
}
func Remove(slice []string, index int) []string {
result := []string{}
result = append(result, slice[0:index]...)
// Append part after the removed element.
result = append(result, slice[index+1:]...)
return result
}
func Insert(s []string, at int, val string) []string {
// Make sure there is enough room
s = append(s, "0")
// Move all elements of s up one slot
copy(s[at+1:], s[at:])
// Insert the new element at the now free position
s[at] = val
return s
}
func RemoveDuplicates(elements []string) []string {
// Use map to record duplicates as we find them.
encountered := map[string]bool{}
result := []string{}
for v := range elements {
if encountered[elements[v]] == true {
// Do not add duplicate.
} else {
// Record this element as an encountered element.
encountered[elements[v]] = true
// Append to result slice.
result = append(result, elements[v])
}
}
// Return the new slice.
return result
}
func MergeStringSlice(slice1, slice2 []string) (c []string) {
c = append(slice1, slice2...)
return
}
func MergeIntSlice(slice1, slice2 []int) (c []int) {
c = append(slice1, slice2...)
return
}
func SliceContains(sl []interface{}, v interface{}) bool {
for _, vv := range sl {
if vv == v {
return true
}
}
return false
other
package other
import (
"fmt"
c "github.com/skilstak/go/colors"
"os"
"reflect"
)
func PrintType(item interface{}) {
Objecttype := reflect.TypeOf(item)
fmt.Println(Objecttype)
}
//credit goes to @whitman
func Spacer(timesToRepeat int) {
//draws that many blank lines
repeat := 0
for repeat < timesToRepeat {
fmt.Println()
repeat++
}
}
func Exit(message string) {
//prints a message and takes in whether you want to clear the screen
fmt.Println(c.B1 + message + c.X)
os.Exit(-1)
}
func ColorTest() {
fmt.Println(c.R + "Red" + c.X)
fmt.Println(c.O + "Orange" + c.X)
fmt.Println(c.Y + "Yellow" + c.X)
fmt.Println(c.G + "Green" + c.X)
fmt.Println(c.C + "Cyan" + c.X)
fmt.Println(c.B + "Blue" + c.X)
fmt.Println(c.M + "Magenta" + c.X)
fmt.Println(c.V + "Violet" + c.X)
fmt.Println(c.B0 + "Base 0" + c.X)
fmt.Println(c.B00 + "Base 00" + c.X)
fmt.Println(c.B1 + "Base 1" + c.X)
fmt.Println(c.B01 + "Base 01" + c.X)
fmt.Println(c.B2 + "Base 2" + c.X)
fmt.Println(c.B02 + "Base 02" + c.X)
fmt.Println(c.B3 + "Base 3" + c.X)
fmt.Println(c.B03 + "Base 03" + c.X)
fmt.Println(c.Multi("MultiColored") + c.X)
fmt.Println(c.Rc() + "Random" + c.X)
}
Dependent Functions
package depend
import "math"
func IndexInt(val, index int) int {
if index > 0 {
val = val / (int)(math.Pow10(index))
}
return val % 10
}
//things that wont work by themselves, used by other functions