Go code examples

Started 12Feb2018, updated 11May2022 (code was broken by a plugin). Alwasy in work

This page is in groups Technology and My Go (golang) notes, and is a blog note with some Go code examples. I have some other Go blog notes, search for «Go» in Technology At the moment the code here does not have any examples of concurrent Go code. I hope to find some great usage and add it later on. The basic idea is to make available any Go code that I «need to» write. I have no plan to make a tutorial, however..

…if you want a really interesting Go code read, try Applied Go [1].

Fold handling

This blog note uses the Collapse-O-Matic WordPress plugin to handle text folding. In addition to expanding and closing them individually you may here:

Expand All
Collapse All

The Go Playground

You can Run the code at the Go Playground. I refer to the code pages for each example.  The code runs at golang.org’s servers. It runs the latest stable release of Go; you can see which one here: https://play.golang.org/p/1VcPUlPk_3. Observe that any time you modify any code in the Playground and ask for a Share link, you get a unique ID. So you can’t destroy my code or anybody else’s, since all new editing constitutes a «branch» to the referenced Playground. In my code examples I have used the Playground’s Format function, it cleans the code layout according to idiomatic Go look and feel, like tabs or block indentation of 8 characters – yes, eight:

Eight (8) char indent or tab

Code here:

package main

import "fmt"

func my_func(do_run bool) bool {
	do_run = !do_run // Just to test if I may modify an input param
	return do_run
}

func main() {
	run_param := false // Or true
	run_now := my_func(run_param)
	fmt.Println(run_now)
}

Simply some coding style

Standard code with some modulo arithmetics

Code here:

package main

import "fmt"

func main() {
	val_max_index := 1000                   // Try val 1,10,100,1000,10000,100000 (with val_num_remainder_of_zeroes_wanted=0)
	val_num_remainder_of_zeroes_wanted := 0 // Try val 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 (with val_max_index=1000)

	remainder_of_zero_cnt := 0

	line_entries_per_line_max := 10 // [2..
	is_running := true
	index := 1                   // Division by zero not legal, start at 1, but print a dot for zero:
	fmt.Print(0, ":\t.", "\t")   // Head of line, start with 0: and dot
	line_vals_this_line_cnt := 1 // Done the first (the dot)

	for is_running { // until index max or reached specified number of zero counts
		remainder := val_max_index % index // remainder after modulo division
		if remainder == 0 {
			remainder_of_zero_cnt++
			if val_num_remainder_of_zeroes_wanted != 0 {
				if remainder_of_zero_cnt == val_num_remainder_of_zeroes_wanted {
					is_running = false
					// Delay printing until after remainder has been printed
				} else {
					// Don't stop now
				}
			} else { // val_num_remainder_of_zeroes_wanted=0
				// Stop when index reaches max
			}
		} else {
			// Not zero now
		}

		fmt.Print(remainder, "\t")

		line_vals_this_line_cnt++

		if line_vals_this_line_cnt != line_entries_per_line_max {
			// More values to print, no new line
		} else {
			line_vals_this_line_cnt = 0
			fmt.Print(" (", remainder_of_zero_cnt, ")\n", index+1, ":\t")
			// End of line, new line with new Head of line, like 10: .. ..  (15)
		}

		if is_running == true {
			is_running = index < val_max_index
			index++
		} else {
			fmt.Print("\n", "Reached num zeroes: ", remainder_of_zero_cnt, " N:", index, " ")
		}
	}
	fmt.Print("\n", "Finally num zeroes: ", remainder_of_zero_cnt)
}

The code basically counts up how many times the remainder after an integer division is zero (no remainder, is divisible), starting at 1 going up to N. The example then does 1000 remainder divsion (%) by 1 up to 1000%1000 (more below). There are some interesting properties that show up there, see references below. There are two things you could easily change, for two test scenarios, the red and the green (somewhat reflected by the code’s colour, above).

In the red test sequence I kept val_num_remainder_of_zeroes_wanted as zero and modified val_max_index as 1,10,100,1000,10000 and saw remainder_of_zero_cnt as 1,4,9,16,25,36. This is actually the first squares: 1*1, 2*2, 3*3, 4*4, 5*5 and 6*6. I wasn’t allowed run time for higher numbers by the Go Playground. (10000 may or may not cause «process took too long» from the Playground server..).

In the green test sequence I kept val_max_index as 1000 and modified val_num_remainder_of_zeroes_wanted as 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 and I got the list of «Reached num zeroes» as 1,2,4,5,8,10,20,25,40,50,100,125,200,250,500,1000. This simply is the list of the possible divisors for 1000.

I found some papers discussing some of this. Like these:

However, it’s the basic look and feel of the code that matters here, not the funny results of the arithmetics. I am used to always showing the else in an if clause; it may clutter the code now, but its intention is clearer. Also since I was used to doing safety critical code at work, there this would be required.

References

Wiki-refs: Modular arithmetic

  1. Applied Go by Christoph Berger, see https://appliedgo.net. Look at the shift register feedback animation at https://appliedgo.net/random/!

Leave a Reply

Dette nettstedet bruker Akismet for å redusere spam. Lær om hvordan dine kommentar-data prosesseres.