Codeground AI
EditorWorkspacesInterviews Meet New Daily Challenges
Data & format
  • JSON DiffCompare two JSON blobs side by side
  • Diff & PatchGenerate unified patches from text/code
  • JSON FormatterPretty-print and validate JSON
  • SQL FormatterFormat SQL and explain with AI
  • JSON ↔ CSVConvert tabular data both ways
  • Base64 CodecEncode and decode Base64
  • Log ParserPretty-print logs and highlight severity
Security & web
  • JWT DebuggerDecode and verify JSON Web Tokens
  • ENV LinterLint .env files and redact values
  • Password GeneratorStrong, configurable passwords
  • UUID GeneratorGenerate UUID v1/v4 in bulk
  • Regex TesterTest patterns in real time
Generators & utilities
  • Epoch ConverterConvert between Unix and dates
  • Meeting PlannerMatrix of slots across timezones
  • Date MathAdd duration with timezone awareness
  • Cron BuilderValidate cron and preview next runs
  • QR GeneratorMake scannable QR codes
  • Color PickerPick & convert colors
  • Lucky Draw WheelSpin-the-wheel utility
Network & creative
  • Speed TestMeasure network throughput
  • Diagram StudioFlowcharts & architecture diagrams
  • Canvas DrawingA scratchpad for sketches
  • Turtle GameCoding game for kids
See everything Codeground AI offers
Reads
Sign In Sign Up
EditorWorkspacesInterviewsMeetDaily ChallengesReads
Tools
JSON DiffDiff & PatchJSON FormatterSQL FormatterJSON ↔ CSVBase64 CodecLog ParserJWT DebuggerENV LinterPassword GeneratorUUID GeneratorRegex TesterEpoch ConverterMeeting PlannerDate MathCron BuilderQR GeneratorColor PickerLucky Draw WheelSpeed TestDiagram StudioCanvas DrawingTurtle Game

Sign InSign Up

Notifications 0

Sending Structs Between Goroutines in Go

Sultan Khan - March 29, 2025


Golang's concurrency model is one of its standout features, enabling developers to build highly concurrent and efficient programs. One common requirement is to send data between goroutines. This blog post will demonstrate how to send a struct from one goroutine to another using channels. We'll use a simple example with a person struct to illustrate the concept.


Introduction

In Go, goroutines are lightweight threads managed by the Go runtime. Channels are the conduits through which goroutines communicate. They provide a way to send and receive values of a specific type between concurrent goroutines. Let's explore how to send a struct via a channel between two goroutines.


Example: Sending a Struct

Here's a complete example in Go where we define a person struct, create two goroutines, and send an instance of the person struct from one goroutine to another via a channel.

The Code

package main

import (
"fmt"
"sync"
)

type person struct {
name string
age int
}

func main() {
var wg sync.WaitGroup
defer wg.Wait()
ch := make(chan person)

wg.Add(2)
go send(ch, &wg)
go receive(ch, &wg)
}

func send(c chan<- person, wg *sync.WaitGroup) {
defer wg.Done()
c <- person{name: "Shubham", age: 19}
close(c)
}

func receive(c <-chan person, wg *sync.WaitGroup) {
defer wg.Done()
p := <-c
fmt.Printf("Received: %+v\n", p)
}

Explanation

  1. Struct Definition: We define a simple person struct with two fields: name and age.
type person struct {
name string
age int
}
  1. Main Function: We initialize a sync.WaitGroup to wait for both goroutines to finish and create a channel to communicate person structs.
func main() {
var wg sync.WaitGroup
defer wg.Wait()
ch := make(chan person)

wg.Add(2)
go send(ch, &wg)
go receive(ch, &wg)
}
  1. WaitGroup: Ensures that the main function waits for both goroutines to complete before exiting.
  2. Channel: A typed channel for sending person structs.
  3. Sending Goroutine: The send function sends a person struct through the channel and then closes the channel.
func send(c chan<- person, wg *sync.WaitGroup) {
defer wg.Done()
c <- person{name: "Shubham", age: 19}
close(c)
}
  1. chan<- person: Indicates that this channel is only for sending person structs.
  2. close(c): Closes the channel after sending the data to signal that no more values will be sent.
  3. Receiving Goroutine: The receive function receives the person struct from the channel and prints it.
func receive(c <-chan person, wg *sync.WaitGroup) {
defer wg.Done()
p := <-c
fmt.Printf("Received: %+v\n", p)
}
  1. <-chan person: Indicates that this channel is only for receiving person structs.

Running the Code

When you run the above code, you should see the following output:

Received: {name:Shubham age:19}

This output demonstrates that the person struct was successfully sent from one goroutine to another via the channel.

Conclusion

Sending structs between goroutines in Go is straightforward with the use of channels. Channels provide a powerful mechanism for communication between goroutines, ensuring that data is safely passed around without the need for explicit locking. This simple example demonstrates the basics, and you can extend this pattern to more complex scenarios in your concurrent Go programs.



Codeground AI

The browser is the only IDE you need. Cloud workspaces, 15+ language runtimes, secure interview tooling and a polished developer toolbox — all in one tab.

Languages

  • Node.js
  • Python
  • Java
  • C++
  • Go
  • Rust
  • TypeScript
  • Web (HTML/CSS/JS)
  • Shell / Bash

Databases

  • MongoDB
  • PostgreSQL
  • MySQL
  • Redis
  • ClickHouse

Tools

  • JSON Diff
  • Diff & Patch
  • JSON Formatter
  • JSON ↔ CSV
  • JWT Debugger
  • Base64 Codec
  • Regex Tester
  • Epoch Converter
  • Cron Builder
  • Meeting Planner
  • SQL Formatter
  • ENV Linter
  • Date Math
  • Log Parser
  • QR Generator
  • UUID Generator
  • Color Picker
  • Password Generator
  • Speed Test
  • Diagram Studio
  • Canvas Drawing
  • Lucky Draw Wheel

Platform

  • Daily Challenges
  • Interviews
  • Reads
  • Turtle (Kids)

Company

  • About Us
  • Privacy Policy
  • Sitemap
  • Contact

© 2026 Codeground AI. Built for developers who want to ship.

About·Privacy·Sitemap·[email protected]