Inverse weighted distance interpolation in golang
Inverse distance weighting (IDW) is a type of deterministic method for multivariate interpolation with a known scattered set of points. The assigned values to unknown points are calculated with a weighted average of the values available at the known points.
This technique, explicitly makes the assumption that things that are close to one another are more alike than those that are farther apart. To predict a value for any unmeasured location, IDW uses the measured values surrounding the prediction location.
basic sorting algorithms implemented in golang
This post includes go-lang based implementation of some of the classic sorting algorithms.
This article primarily, has been written, as an academic exercise, to not forget the basic principles of sorting.
Bubble Sort wiki Bubble sort is perhaps the simplest of all sorting algorithms, the basic principle is to bubble up the largest (or the smallest) and then the second largest and then the third largest and so on. Each bubble takes the full sweep of the list, hence best-case, average-case as well as the worst-case performance are all O(n2)
reading and writing binary files in go lang
Reading and writing binary files can often be a fast and very efficient alternatives to csv. They obviously have their challenges, however in this post I intent to present a very basic example of saving models (struct) into binary file and later reading it.
package main import ( "log" "os" "encoding/binary" "bytes" "fmt" "math/rand" "time" ) //this type represnts a record with three fields type payload struct { One float32 Two float64 Three uint32 } func main() { writeFile() readFile() } func readFile() { file, err := os.
Create linear color gradient in go
Go doesn’t have any builtin gradient functions to paint/fill the background of a raster, however it has all the required primitives one needs to create a very basic linear gradient.
Here is a basic code which defines a 256 x 256 image, then calculates the value of each pixel in the raster based on a linear interpolation formula.
import ( "image" "image/color" "image/png" "log" "os" "os/exec" ) var( colorB = [3]float64{248, 54, 0} colorA = [3]float64{254, 140, 0} ) var( width = 256 height = 256 max = float64(width) ) func linearGradient(x, y float64) (uint8, uint8, uint8) { d := x/max r := colorA[0] + d * (colorB[0] - colorA[0]) g := colorA[1] + d * (colorB[1] - colorA[1]) b := colorA[2] + d * (colorB[2] - colorA[2]) return uint8(r), uint8(g), uint8(b) } func main() { var w, h int = width, height dst := image.
How to read a csv file in go
One of the most common task for a programmer is either to read or write a csv file
Read a csv file package main import ( "bufio" "encoding/csv" "os" "fmt" "io" ) func main() { // Load a csv file. f, _ := os.Open("/path/to/my/csv/file.csv") // Create a new reader. r := csv.NewReader(bufio.NewReader(f)) //define seperator r.Comma = ',' for { record, err := r.Read() // Stop at EOF. if err == io.