0 Hello World

Hello world#

Pre-requisite#

  • You should have go-lang installed on your computer. If not, You should install go-lang by yourself (ref https://go.dev/ ). To check your go-lang version(and confirm you have go-lang installed), open a terminal, type “go version” and then enter, if showing something like “go version go1.21.4 windows/amd64”, then you are fine to continue.

  • You should have an editor for editting the source code. The editor could be VSCode(with this installed, the “code .” will open the current path folder in vscode, as showing in the next step), or notepad++, or vim or sublime.

  • Finally, whenever you meet a problem, you should have perseverance to ask question here and there and find the answer. » Leave comments for impoving this webiste

First go helloworld program#

Make a new folder, make a new file, open in vscode:
(from a terminal)

mkdir 0-step
cd 0-step
touch 0-hello-world.go
code .

Input the simplest content for a go program in 0-hello-world.go and go run it:

package main

func main() {
    println("Hello world")
}
go run ./0-hello-world.go

If the output is “Hello world”, then we are ready. Let’s get started.

The very first gio program#

revise the 0-hello-world.go

the go.mod content should be as below for all the examples in this website:

module 0-hello-world

go 1.20

require gioui.org v0.5.0

require (
	gioui.org/cpu v0.0.0-20210817075930-8d6a761490d2 // indirect
	gioui.org/shader v1.0.8 // indirect
	github.com/go-text/typesetting v0.0.0-20230803102845-24e03d8b5372 // indirect
	golang.org/x/exp v0.0.0-20221012211006-4de253d81b95 // indirect
	golang.org/x/exp/shiny v0.0.0-20220827204233-334a2380cb91 // indirect
	golang.org/x/image v0.5.0 // indirect
	golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 // indirect
	golang.org/x/text v0.7.0 // indirect
)
package main

import (
	"gioui.org/app"
	"gioui.org/op"
)

func main() {
	w := app.NewWindow()
	var ops op.Ops

	for {
		switch e := w.NextEvent().(type) {
		case app.FrameEvent:
			e.Frame(&ops)
		}
	}
}

then

go mod init 0-hello-world
go mod tidy
go run ./0-hello-world.go

(you may need to run go mod tidy before go run ... in next steps if the terminal told you to do so)

It will install some packages (this may cost several minutes, so hold on) and then show a blank window.

That’s it! We will explain what’s happening here.

On closing the window, the terminal will show an error:

fatal error: all goroutines are asleep - deadlock!
...
exit status 2

That’s fine, we will solve it later.

The very first gio program with explaining comments#

package main

import ( // these imports are must have for gio program
	"gioui.org/app"
	"gioui.org/op"
)

func main() {
	w := app.NewWindow() // define a new window instance
                         // it will has NextEvent(), with this you can sent events
						 // so the program could be live
	var ops op.Ops       // ops is a list of operations

	for {                // with this for loop, the program will waiting for
	                     // operations forever and then change the window frame
						 // according to the event it got.
		switch e := w.NextEvent().(type) {      // if the window's next event's type is
		case app.FrameEvent:                    // (is) an app.FrameEvent, this means
		                                        // it's a event for making the window
												// change at the next frame
			e.Frame(&ops)                       // send e.Frame operation to the operation list
                                                // gio will check the operation list every tick
												// and control the window frame properly
		}

        // since the for loop does not exit automatically, the window will always
		// show on the screen

        // when you click the close button of the window, the go routine
		// (defaut go routine set by gio) will exit and the main
		// routine enters asleep and the program
		// get a deadlock and exit
	}
}

That’s all for this simplest gio program.

Obviously the imported app package(with NewWindow method), op package(related to event and operations) are in the core of gio system.

next steps#

Next we are going to work on a window with some static content(showing words “Hello world”), a window with button in material theme, then we will complete this first program and move on to other examples.

» 1-hello-world