2
\$\begingroup\$

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.

GitHub

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
\$\endgroup\$
3
\$\begingroup\$

It's the Go programming language: The Go Programming Language Specification. Using a misnomer like Golang is like spelling and typographical errors in a resume, it's an immediate red flag.

Your packages are undocumented. See Godoc: documenting Go code and GoDoc.

An idiosyncrasy is an unusual feature of a person. It also means odd habit. The term is often used to express eccentricity or peculiarity.

You confess that your libraries are idiosyncratic, which means that they and the code that uses them will be of little use to others.

convert:

You write

func IntToString(num int) string {
    str := strconv.Itoa(num)
    return str
}

which, simplified, is

func IntToString(i int) string {
    return strconv.Itoa(i)
}

It's obvious that IntToString is merely renaming a function from the strconv package in the Go standard library.

s = IntToString(i)
s = strconv.Itoa(i)

Your rationale is "I easily forget some things." That's not a problem. What is a problem is that you don't know how to easily find this information in the documentation. No employer wants employees who write idiosyncratic, unreadable, and unmaintainable code.

Similarly, you write,

func FloatToInt(dec float64) int {
    num := int(dec)
    return num
}

which, simplified, is

func FloatToInt(f float64) int {
    return int(f)
}

It's obvious that FloatToInt is merely performing an int conversion.

i = FloatToInt(f)
i = int(f)

I see negative value in your idosyncratic Go standard library function wrappers.

Similarly the input, mmath, mod, other, and depend packages seem idiosyncratic.

\$\endgroup\$
  • \$\begingroup\$ People often informally call it "Golang" because "Go" is awkward as a noun and difficult to Google. \$\endgroup\$ – 200_success Mar 25 '16 at 17:03
  • 1
    \$\begingroup\$ @200_success: That, and a dozen other excuses :-( \$\endgroup\$ – peterSO Mar 25 '16 at 17:07
  • \$\begingroup\$ Excellent answer. Couldn't agree more. \$\endgroup\$ – jkushner Jul 27 '18 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.