Model data and run concurrent work
Group fields in a struct and coordinate a goroutine with a channel.
- Recognize Go structs, methods, goroutines, and channels.
A struct groups related fields into a named type. Methods attach behavior to a type using a receiver. A go statement starts a goroutine, a lightweight concurrent task. Channels let goroutines send values to one another and coordinate their work.
1package main
2
3import "fmt"
4
5func sendMessage(ch chan string) {
6 ch <- "ready"
7}
8
9func main() {
10 messages := make(chan string)
11 go sendMessage(messages)
12 fmt.Println(<-messages)
13}ready
An unbuffered channel send waits until another goroutine receives the value. This gives the two tasks a clear synchronization point. Start with small examples and keep ownership of shared data simple.
Key takeaways
Recognize Go structs, methods, goroutines, and channels.
Go catches many mistakes while compiling.
Use small experiments to understand each new language feature.
Lesson quiz
4 questions · pass with 3 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Go
Write Go in the editor and run it against sample input with a remote compiler. Your code is sent to Compiler Explorer to compile and run.
Send a message through a channel
Create a string channel, start a goroutine that sends Go is ready, receive the message, and print it.
- Channel message
Go is compiled and run by Compiler Explorer (godbolt.org), a free public service. Your source code and test input are sent to that service.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…