Skip to content

EXAMPLES

Fully functioning examples can be found at github.com/go-loom/loom/examples.

Counter

func Counter() Node {
	count, setCount := Signal(0)

    go func() {
        for {
            time.Sleep(time.Second / 30)
            setCount(count() + 1)
        }
    }()

	return P(Text("Count: "), BindText(count))
}

func Counter() Node {
	count, setCount := Signal(0)

    go func() {
        for {
            time.Sleep(time.Second / 30)
            setCount(count() + 1)
        }
    }()

	return P(Text("Count: "), BindText(count))
}


Conditions

func Condition() Node {
    display, setDisplay := Signal(false)

    toggle := func(*term.EventMouse) {
        setDisplay(!display())
    }

    return Box(
        Box(Text("toggle"), Apply(On{Click: toggle})),

        Show(display, func() Node {
            return Text("am i visible now?")
        }),
    )
}
func Condition() Node {
    display, setDisplay := Signal(false)

    toggle := func(*web.EventMouse) {
        setDisplay(!display())
    }

    return Div(
        Box(Text("toggle"), Apply(On{Click: toggle})),

        Show(display, func() Node {
            return Text("am i visible now?")
        }),
    )
}

Lists

func FruitList() Node {
    fruits, setFruits := Signal([]string{"banana", "apple", "orange"})

    return Box(
        For(fruits, func(fruit string, index Accessor[int]) Node {
            return P(Text(fruit))
        }),
    )
}
func FruitList() Node {
    fruits, setFruits := Signal([]string{"banana", "apple", "orange"})

    return Ul(
        For(fruits, func(fruit string, index Accessor[int]) Node {
            return P(Text(fruit))
        }),
    )
}

Goroutine cancellation

See Self().

func MyComponent() Node {
    go func(self Component) {
        for {
            select {
            case <-self.Disposed():
                // stop Goroutine when component is diposed
                return
            default:
            }

            // keep looping
        }
    }(Self())

	return Text("My component")
}
func MyComponent() Node {
    go func(self Component) {
        for {
            select {
            case <-self.Disposed():
                // stop Goroutine when component is diposed
                return
            default:
            }

            // keep looping
        }
    }(Self())

	return Text("My component")
}