Getting started with TinyGo and WebAssembly (WASM)

Posted on Fri 23 October 2020 in Development • Tagged with go, golang, tinygo, web, webassembly, wasm, assembly, js, javascript, html, browser, macos, code, development, embedded, programming, language

This tutorial explains how to call a method written in Go and compiled to WebAssembly (WASM) from JavaScript by using TinyGo


Continue reading

Goenv - Go Environment Manager

Posted on Sun 19 April 2015 in Go, HowTo, Programmazione • Tagged with go, golang

To briefly explain what Goenv is, I will assume you have previously worked with Python. Basically it's what Virtualenv is for Python. Goenv (and it's wrapper goof) creates a folder for a new project and set the $GOPATH env variable to that folder path. At this point every time you do go get, the libraries will be installed in that specific $GOPATH.

It's very important to use separate $GOPATH for each project, because this allow us to use different library versions for each project and avoid version conflicts.

Installation

git clone https://bitbucket.org/ymotongpoo/goenv
cd goenv
go build -o goenv *.go
chmod +x goenv
sudo cp goenv /usr/bin

Goenv is now installed, we will now install its wrapper goof:

sudo cp shellscripts/goenvwrapper.sh /usr/bin

Edit .bashrc (or .zshrc if you use zsh) and append these lines:

export GOENVHOME=$HOME/goenvs
source /usr/bin/goenvwrapper.sh

How to use it

To create a new go environment use make:

goof make go-test
Do you want to create all parental directory of '/Users/andrea/goenvs/go-test'? [y/N]: y
Environment /Users/andrea/goenvs/go-test created!
(go:go-test) ➜  go-test

To exit the go environment use deactivate:

(go:go-test) ➜  go-test  deactivate
go-test

To use an environment use workon:

go-test  goof workon go-test
(go:go-test) ➜  go-test

To show available environments use show:

(go:go-test) ➜  go-test  goof show
go-test

Goenv itself is not enough to manage Go packages. It would be like using Virtualenv only and not using pip and requirements. In a future post I will explain how to use Godep.


Go: defining methods on struct types

Posted on Mon 16 March 2015 in Go • Tagged with go, golang, programming, struct

In Go it's possible to define methods on struct types. The syntax needed for it can be a bit strange for people that are used to define classes and methods in Java, C# etc... but once you learn it it's quite easy to use.

In my case for example I needed something that could contain a Timer object, a string and a method that could start the timer and call a method at the end of the Timer execution. I implemented it in this way:

type DeviceTimer struct {
    DeviceID    string
    DeviceTimer *time.Timer
}

func (timer DeviceTimer) startTimer() {
    <-timer.DeviceTimer.C
    notifyDeviceTimerExpired(timer.DeviceID)
}

The key point is row 6 func (timer DeviceTimer) startTimer() { ... } where I defined a method called startTimer and I specify timer DeviceTimer inside the func definition. This basically "extends" the struct DeviceTimer adding that method to it. This means that I can call that method in this way:

timer := time.NewTimer(time.Millisecond * 300)
device_timer := DeviceTimer{"abc123", timer}
go device_timer.startTimer()

This is all you need to do. If you want to read more about this subject, I can suggest to read these two articles:

Note: I'm not a Go expert and these are just my personal notes I'm taking during my learning experience. I'm very keen to share my notes with everyone, but please don't take them as notes from an expert Go developer.