Home  >  Article  >  Backend Development  >  How to write interface program in golang

How to write interface program in golang

下次还敢
下次还敢Original
2024-04-21 01:03:21354browse

How to use Go to write interface programs

Introduction
Go is a popular programming language that is mainly used for server-side development. However, Go also supports the use of third-party libraries to create programs with graphical user interfaces (GUIs). This article will introduce how to use Go to write interface programs.

Step 1: Choose a GUI library
First, you need to choose a library for creating GUI. Popular GUI libraries available in Go include:

  • [GoGi](https://github.com/go-kirin/go-kirin)
  • [Fyne](https: //github.com/fyne-io/fyne)
  • [Glui](https://github.com/AllenDang/glui)

Step 2: Installation Libraries
Install the required GUI libraries using the Go module system:

<code>go mod tidy</code>

Step 3: Create the window
Next, create the window using the selected GUI library :

GoGi Example:

<code class="go">import (
    "github.com/go-kirin/go-kirin"
)

func main() {
    win := kirin.NewWindow("Hello World", 600, 400)
    win.Show()
    win.Run()
}</code>

Fyne Example:

<code class="go">import (
    "github.com/fyne-io/fyne"
)

func main() {
    app := fyne.NewApp("Hello World")
    app.SetIcon(resourceAppIconPng)
    w := app.NewWindow("Hello World")
    w.Resize(fyne.Size{Width: 600, Height: 400})
    w.ShowAndRun()
}</code>

Step 4: Add Control
Then, add controls to the window, such as buttons, labels, and text input boxes:

GoGi example:

<code class="go">func main() {
    ...
    button := kirin.NewButton("Click Me")
    label := kirin.NewLabel("Hello, World!")
    edit := kirin.NewTextBox()
    win.SetChild(kirin.Stack().AddChildren(button, label, edit))
    ...
}</code>

Fyne example:

<code class="go">func main() {
    ...
    button := fyne.NewButton("Click Me")
    label := fyne.NewLabel("Hello, World!")
    edit := fyne.NewEntry()
    w.SetContent(fyne.Container.NewVBox(button, label, edit))
    ...
}</code>

Step 5: Handling Events
Finally, user events can be handled, such as button clicks or text input:

GoGi Example:

<code class="go">func main() {
    ...
    button.OnClicked(func(e *kirin.MouseEvent) {
        label.SetText("Clicked!")
    })
    ...
}</code>

Fyne Example:

<code class="go">func main() {
    ...
    button.OnTapped = func() {
        label.SetText("Clicked!")
    }
    ...
}</code>

The above is the detailed content of How to write interface program in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn