Adventures in CS

Golang snippets

03 Mar 2019

Go code snippets and libraries

Updated on: 03-03-2019

Deleting an element from a slice

Let’s say that the element you want to delete is on index ‘i’ then

a = append(a[:i], a[i+1:]...)

Sorting in Golang

This is sorting an array of structure planets according to the axis value of the struct

slice.Sort(planets[:], func(i, j int) bool {
    return planets[i].Axis < planets[j].Axis

### Serialization library
If you want to send data from one node to another you will likely need some serialization/deserialization. Either use the [encoding/gob]( library or [struc]( library. For more information take a look at [this tutorial for gob](

Here's a snippet of code for gob:

package main
import (
var m = map[string]int{"one":1, "two":2, "three":3}
func main() {
b := new(bytes.Buffer)
e := gob.NewEncoder(b)
// Encoding the map
err := e.Encode(m)
if err != nil {
var decodedMap map[string]int
d := gob.NewDecoder(b)
// Decoding the serialized data
err = d.Decode(&decodedMap)
if err != nil {
// Ta da! It is a map!
fmt.Printf("%#v\n", decodedMap)

Short intro to Interfaces

Checkout this page from go by example to get a basic idea about how to work with Interfaces in go.

Number parsing examples

Here’s a snippet from go by example. Here’s the Go playground link

package main
// The built-in package `strconv` provides the number
// parsing.
import "strconv"
import "fmt"
func main() {
    // With `ParseFloat`, this `64` tells how many bits of
    // precision to parse.
    f, _ := strconv.ParseFloat("1.234", 64)
    // For `ParseInt`, the `0` means infer the base from
    // the string. `64` requires that the result fit in 64
    // bits.
    i, _ := strconv.ParseInt("123", 0, 64)
    // `ParseInt` will recognize hex-formatted numbers.
    d, _ := strconv.ParseInt("0x1c8", 0, 64)
    // A `ParseUint` is also available.
    u, _ := strconv.ParseUint("789", 0, 64)
    // `Atoi` is a convenience function for basic base-10
    // `int` parsing.
    k, _ := strconv.Atoi("135")
    // Parse functions return an error on bad input.
    _, e := strconv.Atoi("wat")

Generating a slice with sequence of numbers:

Here’s a function to generate sequence of numbers:

func makeRange(min, max int) []int {
    a := make([]int, max-min+1)
    for i := range a {
        a[i] = min + i
    return a

Link to Go playground

Useful Libraries:

  1. Numpy-like functionality: gonum
  2. Working with and modifying HTML files? Take a look at template package



Tweet me @mb_ce if you like this post.

comments powered by Disqus