[Golang] Notes on frequently used functions

About this article

Since I wrote it completely for myself, most of the explanations are small and easy processing, but I will update it as needed because it is used as a memorandum.

Cast from string

stringint

func atoi(a string) (b int) {
    b, err := strconv.Atoi(a)
    if err != nil {
        panic(err)
    }
    return
}

Half-width space delimited string [] string

func aToSlice(a string) (b []string) {
    b = strings.Fields(strings.TrimSpace(a))
    return
}

Half-width space-separated string [] int

func aToIntSlice(a string) (b []int) {
    c := strings.Fields(strings.TrimSpace(a))
    for _, v := range c {
        d := atoi(v)
        b = append(b, d)
    }
    return
}

String [] float64 separated by half-width spaces

func aToFloat64Slice(a string) (b []float64) {
    c := strings.Fields(strings.TrimSpace(a))
    for _, v := range c {
        d, _ := strconv.ParseFloat(v, 64)
        b = append(b, d)
    }
    return
}

Cast from slice to another type of slice

[]string[]int

func toIntSlice(a []string) (b []int) {
    for _, s := range a {
        i := atoi(s)
        b = append(b, i)
    }
    return
}

[]int[]string

func toStrSlice(a []int) (b []string) {
    for _, i := range a {
        s := strconv.Itoa(i)
        b = append(b, s)
    }
    return
}

Other

Returns the absolute value as int

func abs(a int) (b int) {
    b = int(math.Abs(float64(a)))
    return
}

Returns the maximum value of value from map [int] int

func findMaxValue(m map[int]int) (maxValue int) {
    var max int
    var maxIndex int
    for i, v := range m {
        if max <= v {
            max = v
            maxIndex = i
        }
    }
    maxValue = m[maxIndex]
    return
}

Returns a slice of only the key with the maximum value from map [int] int

Go's associative array (map) is sorted and returned in ascending order because the order is different for each execution when using a range loop.

func findMaxValueKeys(m map[int]int) (maxKeys []int) {
    maxValue := findMaxValue(m)
    
    for i, v := range m {
        if v == maxValue {
            maxKeys = append(maxKeys, i)
        }
    }
    sort.Slice(maxKeys, func (i, j int) bool { return maxKeys[i] < maxKeys[j] })
    return
}

Recommended Posts

[Golang] Notes on frequently used functions
Notes on SciPy.linalg functions
Frequently used (personally) notes on the tar command
Notes on python personally used things (input, trigonometric functions, logarithms)
Notes on Flask
Golang on jupyter
List of frequently used built-in functions and methods
pyenv Frequently used commands
Frequently used tmux commands
Notes on neural networks
Celery notes on Django
Frequently used linux commands
Notes on installing PycURL
Display a list of frequently used commands on Zsh
Notes on using Alembic
Frequently used pip commands
Notes on tf.function and Tracing
Notes on installing dlib on mac
Notes on python's sqlite3 module
Notes on * args and ** kargs
Notes on defining PySide slots (2)
[Django] Notes on using django-debug-toolbar
Notes on pyenv and Atom
Notes on defining PySide slots
[Python] Notes on data analysis
[Python] Frequently used library code
Notes on optimization using Pytorch
Notes on installing Python on Mac
Notes on studying multidimensional scaling
Frequently used subpackages of SciPy
Python frequently used code snippets
Frequently used commands in virtualenv
Notes on installing pipenv on Mac
Notes on installing Anaconda 3 on Windows
Notes on imshow () in OpenCV
Notes on installing Python on CentOS