Learning Go Episode 3 – More Data Types, Casting, Rendering an SVG file, and writing to Files.

Episode Post & Video Links:  1, 2, 3 (this post), 4, 5, 6, 7, and 8. Non-linked are in the works! Videos available now on Youtube however, so check em’ out!

Episode 3 of my recurring “Learning Go” Saturday stream really got more into the particulars of Go data types including integers, strings, more string formatting verbs, concatenation, type casting, and lots of other pedantic details. In the episode I also delve into some OBS details with the audience, and we get the Twitch interface I’ve setup a bit more streamlined for easier readability. Overall, I think it’s looking much better than just the last episode! Hats off to the conversational assist from the audience.

Here’s the play by play of what was covered in episode 3 with the code in gists plus the repo is available on Github. Video below the timeline.

Timeline

0:00 Intro
6:08 The point I fix the sound. Just skip that first bit!
6:24 Re-introducing the book I’m using as a kind of curriculum guide for these go learning sessions.
7:44 Quick fix of the VM, a few updates, discussion of Goland updates, and fixing the Material Theme to a more visually less caustic theme. Also showing where it is in the IDE.
9:52 Getting into the learning flow, starting a new project with Go 1.11.4 using the Goland IDE new project dialog.


package main
import (
"fmt"
"math"
"strconv"
)
func main() {
var someInt int
var anotherValue int64
thisValue := 45
thisVAlue, thatValue, someValue := 20, 23, 15
someInt = thisVAlue
anotherValue = 64
const knownSet = 7.12904
const anotherKnown = 9.10347
for x := 0; x < 20; x++ {
fmt.Printf("x = %d e = %8.3f\n", x, math.Exp(float64(x)))
}
fmt.Printf("Known Set and Another Known: %2.7f, %2.7f", knownSet, anotherKnown)
fmt.Printf("The values: %6d, %6d, %6d, and %s.\n", thisValue, thisVAlue, thatValue, strconv.Itoa(someValue))
fmt.Printf("Other values: %4d, %4d.\n", someInt, anotherValue)
fmt.Printf("Function doThings: %6d\n", doThings(thisVAlue, thisValue))
fmt.Printf("Function doMoreThings: %s", doMoreThings(someInt, int(anotherValue), "Adron"))
}
func doThings (valueOne int, valueTwo int) int64 {
result := valueOne * valueTwo
return int64(result)
}
func doMoreThings (valueOne int, valueTwo int, name string) string {
result := valueTwo + valueOne * 10
return "The result added and exponentially multiplied for you " + name + " this: " + strconv.Itoa(result)
}

view raw

main.go

hosted with ❤ by GitHub

10:50 Creating the Github repo for learning-go-episode-3.
12:14 Setting up the initial project and CODING! Finally getting into some coding. It takes a while when we do it from nothing like this, but it’s a fundamentally important part of starting a project!
13:04 From nothing, creating the core basic elements of a code file for go with main.go. In this part I start showing the various ways to declare types, such as int and int64 with options on style.
14:14 Taking a look at printing out the various values of the variables using formatter verbs via the fmt.Printf function.
17:00 Looking at converting values from one type to another type. There are a number of ways to do this in Go.

I also, just recently, posted a quick spot video and code (blog entry + code) on getting the minimum and maximum value in Go for a number of types. This isn’t the course video, just a quick spot. Keep reading for the main episode below.

18:16 Oh dear the mouse falls on the ground. The ongoing battle of streaming, falling objects! But yeah, I get into adding a function – one of the earlier functions being built in the series – and we add a signature with a return int64 value. I continue, with addition of another function and looking at specifics of the signature.
25:50 Build this code and take a look at the results. At this point, some of the formatting is goofed up so I take a look into the formatter verbs to figure out what should be used for the output formatting.
33:40 I change a few things and take a look at more output from the various calculations that I’ve made, showing how various int, int64, and related calculations can be seen.
37:10 Adding a constant, what it is, and when and where and why to declare something as a constant.
38:05 Writing out another for loop for output results of sets.
42:40 A little git work to create a branch, update the .gitignore, and push the content to github. Repo is here btw: https://github.com/Adron/learning-go-episode-3

At this point I had to take a short interruption to get my ssh keys setup for this particular VM so I could push the code! I snagged just a snippet of the video and made a quick spot video out of it too. Seems a useful thing to do.

47:44 Have to add a new ssh key for the virtual machine to github, so this is a good little snippet of a video showing how that is done.
56:38 Building out a rendering of an SVG file to build a graphic. The complete snippet is below, watch the video for more details, troubleshooting, and working through additions and refactoring of the code.


package main
import (
"fmt"
"io/ioutil"
"math"
"strconv"
)
const (
width, height = 600, 600
cells = 100
xyrange = 30.0
xyscale = width / 2 / xyrange
zscale = height * 0.4
angle = math.Pi / 6
)
var sin30, cos30 = math.Sin(angle), math.Cos(angle)
func main() {
var outputResult string
outputResult = "<svg xmlns='http://www.w3.org/2000/svg&#39; " +
"style='stroke: blue; fill: white; stroke-width: 0.7' " +
"width='" + strconv.Itoa(width) + "' height='" + strconv.Itoa(height) + "'>"
for i := 0; i < cells; i++ {
for j := 0; j < cells; j++ {
ax, ay := corner(i+1, j)
bx, by := corner(i, j)
cx, cy := corner(i, j+1)
dx, dy := corner(i+1, j+1)
outputResult = outputResult + "<polygon points='" +
ax + "," + ay +
bx + "," + by +
cx + "," + cy +
dx + "," + dy +
"'/>\n"
}
}
outputResult = outputResult + "</svg>"
fmt.Printf(outputResult)
err := ioutil.WriteFile("surface-plot.svg", []byte(outputResult), 0644)
if err != nil {
fmt.Printf("Error: %s", err)
}
}
func corner(i, j int) (string, string) {
x := xyrange * (float64(i)/cells – 0.5)
y := xyrange * (float64(j)/cells – 0.5)
z := f(x, y)
sx := width/2 + (x-y)*cos30*xyscale
sy := height/2 + (x+y)*sin30*xyscale – z*zscale
xResult := strconv.FormatFloat(sx, 'f', -1, 64)
yResult := strconv.FormatFloat(sy, 'f', -1, 64)
return xResult, yResult
}
func f(x, y float64) float64 {
r := math.Hypot(x, y)
return math.Sin(r)
}

view raw

main.go

hosted with ❤ by GitHub

1:15:32 We begin the mission of bumping up the font size in Goland. It’s a little tricky but we get it figured out.
1:33:20 Upon realization, we need to modify for our work, that this outputs directly to a file instead of just the console. Things will work better that way so I work into the code a write out to file.
1:40:05 Through this process of changing it to output to file, I have to work through additional string conversions, refactoring, and more. There’s a lot of nuance and various things to learn during this section of the video, albeit a little slow. i.e. LOTS of strconv usage.
2:01:24 First view of the generated SVG file! Yay! Oh dear!
2:09:10 More troubleshooting to try and figure out where the math problem is!
2:22:50 Wrapping up with the math a little off kilter, but sort of fixed, I move on to getting a look into the build but also pushing each of the respective branches on github. Repo is here btw: https://github.com/Adron/learning-go-episode-3

Art, JavaScript, and Machine Learning with Amy Cheng at ML4ALL 2018

One talk that opened my mind to new ideas about where, how, and when to use machine learning was Amy Cheng’s @am3thyst talk on Machine Learning, Art, and JavaScript. I introduced her last year in my previous post, and am linking the talk below. Give it a watch, it’s worth the listen!

ML4ALL 2019 is on. CFP is still open, a little longer. It’s closing on the 18th. In addition we’ve got tickets available for early birds, but those will be gone soon too so pick one up while you can, it’s only a $200.00 bucks. You’ll basically be getting a ticket to conference that’ll be 10x the value of one of these big corporate conferences for $200 bucks, in the awesome city of Portland, and I can promise you it’ll be a conference you’ll get more out of then you currently think you will! Join us, it’s going to be a great time!

Thrashing Code Metal Monday for Week of March 4th – Get Awoke! \m/

A while back I was hashing through a number of metal Monday posts, it was time to get em’ started again. Here’s a few of my latest listens to help you awake this Monday and get into some thrashing code.

First off, again throwing this band into the mix, with incredible vocals that often shock people, Brittney Hayes (A.K.A. Brittney Slayes) tears into the sound barrier with inspiring awe backed by Scott Buchanen, Grant Truesdell, and Andrew Kingsley with virtuoso slaying! Learn more about Unleash the Archers or check out some of the following epic masterpieces that I’ve provided you here.

Next up a seriously wicked awesome band from beloved Ukraine from the city of Horlivka! Jinjer is lead by Tatiana Shmailyuk with Roman Ibramkhalilov, Eugene Abdiukhanov, and Vladislav Ulasevich. This band combines a varying twist of vocal melody and brashness, with extreme control from Tatiana and a diver set of melody and harmony among the strings, with prodding rhythm from the drums.

1600px-JINJER_–_Promo_2018 - slice.jpg

Since I’m going for a lucky three every Thrashing Code Metal Monday, here’s the premium curated third choice! This is an entirely new band I discovered just this past Sunday while watching the sun burn violently an etching across the usually overcast peaceful sky. Oceans of Slumber are different in many ways compared to much of the metal I’ve been listening to as of late, but have a huge variety of influence and a lower range that I in enraptured with.

The band is made of lead singer Cammie Gilbert and backed with Keegan Kelly and Dobber Beverly. It’s some slower moving, passion inducing surrealist art that flows along like the seasons.

Just for good measure, check out Sunlight live in studio with Oceans of Slumber too and also the absolute epic combination of video, art, music, and passion in No Color, No Light!

Past Thrashing Code Metal Mondays for your listening pleasure.

 

A Short Spot on Coding up Go Types Sizing Limits

In an effort, finally, to get around to putting more content together I’ve created this video. It’s the first of many that I’ll put together. This one is a quick coding example of getting the maximum size of particular types in Go. At just about a minute, it’s a quick way to pick up a way to do something coding. If you like this video, do leave a comment, if you thought it wasn’t useful leave a comment. Either way I’d like to read the over under on material like this and if it is or isn’t useful (or entertaining) for you. It’ll mostly be just little tips n’ tricks on how to get things done.

The code gist.


package main
import (
"fmt"
"math"
)
const(
MinUint uint = 0 // all zeroes
// Perform a bitwise NOT to change every bit from 0 to 1.
MaxUint = ^MinUint // all ones
// Shift the binary number to the right to get the high bit to zero
MaxInt = int(MaxUint >> 1) // all ones except high bit
MinInt = ^MaxInt // all zeroes except high bit.
)
func main() {
// integer max
fmt.Printf("max for int64 = %+v\n", math.MaxInt64)
fmt.Printf("max for int32 = %+v\n", math.MaxInt32)
fmt.Printf("max for int16 = %+v\n", math.MaxInt16)
fmt.Printf("max for int8 = %+v\n", math.MaxInt8)
// integer min
fmt.Printf("min for int64 = %+v\n", math.MinInt64)
fmt.Printf("min for int32 = %+v\n", math.MinInt32)
fmt.Printf("min for int16 = %+v\n", math.MinInt16)
fmt.Printf("min for int8 = %+v\n", math.MinInt8)
// Added parts that aren't in the video!
fmt.Printf("max float64 = %+v\n", math.MaxFloat64)
fmt.Printf("max float32 = %+v\n", math.MaxFloat32)
fmt.Printf("smallest nonzero float64 = %+v\n", math.SmallestNonzeroFloat64)
fmt.Printf("smallest nonzero float32 = %+v\n", math.SmallestNonzeroFloat32)
fmt.Printf("max uint = %+v\n", MaxUint)
fmt.Printf("min uint = %+v\n", MinUint)
fmt.Printf("max int = %+v\n", MaxInt)
fmt.Printf("min int = %+v\n", MinInt)
}

view raw

main.go

hosted with ❤ by GitHub

 

Machine Learning, Protocols, Classification, and Clustering

Today Suz Hinton @noopkat and Amanda Moran @AmandaDataStax are presenting, “Alternative Protocols – how offline machines can still talk to each other” and “Classification and Clustering Algorithms paired with Wine and Chocolate” respectively. The aim is to stream these talks tonight too on my Thrashing Code Twitch Channel. If you can attend in person, we’re almost at capacity so make sure you snag one of the remaining RSVP’s.

Here’s some more details on the speakers for tonight.

Continue reading “Machine Learning, Protocols, Classification, and Clustering”