Deprecated: Array and string offset access syntax with curly braces is deprecated in /customers/1/2/4/mrwaggel.be/httpd.www/admin/kernel/helpers/text.class.php on line 171 Notice: Trying to access array offset on value of type null in /customers/1/2/4/mrwaggel.be/httpd.www/admin/kernel/db/db_posts.class.php on line 390 Notice: Trying to access array offset on value of type null in /customers/1/2/4/mrwaggel.be/httpd.www/admin/kernel/db/db_pages.class.php on line 337 Notice: Trying to access array offset on value of type null in /customers/1/2/4/mrwaggel.be/httpd.www/admin/kernel/db/db_comments.class.php on line 283 Deprecated: Function get_magic_quotes_runtime() is deprecated in /customers/1/2/4/mrwaggel.be/httpd.www/admin/boot/rules/4-remove_magic.bit on line 18 Deprecated: Function get_magic_quotes_gpc() is deprecated in /customers/1/2/4/mrwaggel.be/httpd.www/admin/boot/rules/4-remove_magic.bit on line 23 Golang - Mr.Waggel's blog

Golang human readable byte sizes

31 March, 2020

Here is a function that prints out byte sizes, like the return from len() function, into human readable units.

Function and example

package main

import (
	"fmt"
	"strconv"
)

const (
	TB = 1000000000000
	GB = 1000000000
	MB = 1000000
	KB = 1000
)

func lenReadable(length int, decimals int) (out string) {
	var unit string
	var i int
	var remainder int

	// Get whole number, and the remainder for decimals
	if length > TB {
		unit = "TB"
		i = length / TB
		remainder = length - (i * TB)
	} else if length > GB {
		unit = "GB"
		i = length / GB
		remainder = length - (i * GB)
	} else if length > MB {
		unit = "MB"
		i = length / MB
		remainder = length - (i * MB)
	} else if length > KB {
		unit = "KB"
		i = length / KB
		remainder = length - (i * KB)
	} else {
		return strconv.Itoa(length) + " B"
	}

	if decimals == 0 {
		return strconv.Itoa(i) + " " + unit
	}

	// This is to calculate missing leading zeroes
	width := 0
	if remainder > GB {
		width = 12
	} else if remainder > MB {
		width = 9
	} else if remainder > KB {
		width = 6
	} else {
		width = 3
	}

	// Insert missing leading zeroes
	remainderString := strconv.Itoa(remainder)
	for iter := len(remainderString); iter < width; iter++ {
		remainderString = "0" + remainderString
	}
	if decimals > len(remainderString) {
		decimals = len(remainderString)
	}

	return fmt.Sprintf("%d.%s %s", i, remainderString[:decimals], unit)
}

func main() {
	test := []int{425005, 8741208, 114448910, 557891, 557,
		114710, 8933578, 5849684981, 12033687, 742289, 678007439}

	for _, v := range test {
		fmt.Println(v, "\t = ", lenReadable(v, 2))
	}
}

Output

425005          =  425.00 KB
8741208         =  8.74 MB
114448910       =  114.44 MB
557891          =  557.89 KB
557             =  557 B
114710          =  114.71 KB
8933578         =  8.93 MB
5849684981      =  5.84 GB
12033687        =  12.03 MB
742289          =  742.28 KB
678007439       =  678.00 MB

Golang serving resume able file downloads with net/http

30 March, 2020

Here is a fully commented example golang function on how to serve and transmit files to clients with the go net/http package. It supports the "Accept-Ranges" header, which allows for downloads to be paused or interrupted and resumed later on. This is very useful for clients who don't have stable connections to download larger files. However this function does not support Multipart downloads, if you wish to have that functionality check this function in the net/http source code for implementation.

This function is written with the following sources RFC 7233 section 3.1MDN Range Requests and Content Range.

Jetbrains Webstorm chrome live edit fix

29 March, 2020

I was encountering problems with Jetbrains not connecting to chrome IDE Support extension. For me the simple fix was setting the listener port of the extension to the correct value.

In chrome go the extension options page and set the port to.

63343

undefined

Now close chrome, go the IDE and hit that debug button, and behold the mighty message;

undefined

 

Golang reflect if initialized struct has member method or fields

12 April, 2018

In Go it is possible to check if any pointer to an initialized struct has a given method or field. The use of this is to make sure that you can call a given method, or access a given field without receiving a runtime panic.

Serialize structures in Golang

04 April, 2018

In this blogpost I'll show you one of the many ways to serialize structures in Go.

Golang Beego implementing and using CAPTCHA

19 January, 2017

In this small tutorial I'll show you how to use CAPTCHA validation inside your application. We won't be using the builtin CAPTCHA of Beego but instead use another package of GitHub user dchest, and his package captcha.

Home ← Older posts