Hello World
Create a simple Hello World example app with Gi.
Categories:
Create a new Go project
- Navigate back to your home directory by running
cd
- Create a new directory called myapp by running
mkdir myapp
- Navigate to your newly created directory by running
cd myapp
- Create a new Go module by running
go mod init myapp
- Create a new Go file by running
touch main.go
- Open main.go using an editor of your choice
Make a simple app
Add the following code to your editor:
package main import ( "github.com/goki/gi/gi" "github.com/goki/gi/gimain" ) func main() { // Run the window event loop function as the main function gimain.Main(func() { mainrun() }) } func mainrun() { // Create a window called My App Window with width 512 and height 384 win := gi.NewMainWindow("myapp", "My App Window", 512, 384) // Get the viewport within the window vp := win.WinViewport2D() // Start a protect update on the viewport updt := vp.UpdateStart() // Create a standard frame within the window and make it the main widget mfr := win.SetMainFrame() // Add a label to the main frame with the text "Hello, World!" label := gi.AddNewLabel(mfr, "label", "Hello, World!") // Make the font size of the label large label.SetProp("font-size", "large") // End the protected update on the viewport without a signal. // Update signals cause things to be redrawn, which is unnecessary at the start // because it is already drawing everything new. vp.UpdateEndNoSig(updt) // Start the event loop that keeps the window rendering. // This is a blocking call, and it will not return until // the user quits the app or gi.Quit() is called win.StartEventLoop() }
Update your dependencies by running
go mod tidy
Build the code by running
go build
Run the app by running
./myapp
if you are on MacOS or Linux and./myapp.exe
if you are on Windows. This should create a window with text that says “Hello, World,” similar to the screenshot below:
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.
Last modified January 22, 2024: removed old deps from go mod (0b4edcc)