25% off

Use code FUNCMAIN at checkout for 25% off all premium courses.

Get started →

Channels in Go

June 4, 2021

In this video, we are going to be looking at Channels in Go and what they are used for.

Overview

package main

import (
	"fmt"
	"math/rand"
)

func CalculateValue(values chan int) {
	value := rand.Intn(10)
	fmt.Printf("Value Calculated: %d\n", value)
	values <- value
}

func main() {
	values := make(chan int)
	go CalculateValue(values)

	value := <-values
	fmt.Println(value)
}

Output

$ go run main.go
Value Calculated: 1
1
Elliot Forbes

Elliot Forbes

Course Instructor

Hey Gophers! My name is Elliot and I'm the creator of TutorialEdge and I've been working with Go systems for roughly 5 years now.