Basics
The basics are a simple overview of the key concepts of Cogent Core. You can interactively run and edit the examples on this website directly, or you can install Cogent Core on your system and build locally. You can also use the playground to develop interactively. After you finish the basics, we recommend you read the tutorials and explore the widgets.
Hello world
Main page: Hello world tutorial
This tutorial makes a simple hello world example app:
package main
import "cogentcore.org/core/core"
func main() {
b := core.NewBody()
core.NewButton(b).SetText("Hello, World!")
b.RunMainWindow()
}Notice how you can see the result of the code above, a button with the text “Hello, World!”. Not only can you see the result of the code, you can edit the code live. Try changing “Hello, World!” to “Click me!” and you will see the button update accordingly. Main page: App The first call in every app is core.NewBody. This creates and returns a new body, which is a container in which app content is placed. This takes an optional name, which is used for the title of the app/window/tab. After calling NewBody, you add widgets to the body that was returned, which is typically given the local variable name Then, after adding content to your body, you can create and start a window from it using core.Body.RunMainWindow. Therefore, the standard structure of an app looks like this: For most of the code examples on this website, we will omit the outer structure of the app so that you can focus on the app content. Main page: Widget All app content is organized into widgets, which are reusable app components that render, store information, and handle events. See widgets for a list of widget types. All widgets satisfy the core.Widget interface. Widgets are typically created by calling the Many widgets define attributes that you can set, like the text of a button. These attributes can be set using the Here is an example of using You can always assign a widget to a variable and then get information from it or make further calls on it at any point. For example:App
b for body.package main
import "cogentcore.org/core/core"
func main() {
b := core.NewBody("App Name")
// Add app content here
b.RunMainWindow()
}Widget
core.New{WidgetName} function (for example: core.NewButton). All of these New functions take a parent in which the widget is added. This allows you to create nested widget structures and layouts that position and size widgets in different ways. For elements at the root level of your app, the parent is b, the app body. However, if your widget is located in a some other container, you would pass that as the parent.Set{AttributeName} method (for example: core.Button.SetText). These Set methods always return the original object so that you can chain multiple Set calls together on one line. You can also always access the attributes of a widget by directly accessing its fields.New and Set functions to construct and configure a widget:core.NewButton(b).SetText("Click me!").SetIcon(icons.Add)bt := core.NewButton(b).SetText("Click me!")
// Later...
bt.SetText("New text")