ADVERTISEMENT
ADVERTISEMENT

Golang close() Function with Examples

Golang | close() Function: Here, we are going to learn about the built-in close() function with its usages, syntax, and examples.
Submitted by IncludeHelp, on October 14, 2021

close() Function

In Go programming language, the close() is a built-in function that is used to close a channel, and the channel must be either bidirectional or send-only and should be executed only by the sender, never the receiver, and has the effect of shutting down the channel after the last sent value is received. After the last value has been received from a closed channel c, any receive from c will succeed without blocking, returning the zero value for the channel element. The form is: x, ok := <-c It will also set ok to false for a closed channel.

It accepts one parameter (c chan<- Type) and returns nothing.

Reference: Golang close() function

Syntax:

func close(c chan<- Type)

Parameter(s):

  • c : The channel to be closed

Return Value:

The return type of the close() function is none.

Example 1:

// Golang program to demonstrate the
// example of close() function

package main

import (
	"fmt"
)

func main() {
	// Creating a channel using make()
	channel1 := make(chan string)

	// Printing type and value
	fmt.Printf("%T, %v\n", channel1, channel1)

	// Anonymous goroutine
	go func() {
		channel1 <- "Hello, world!"
		channel1 <- "Hi, friends!"
		channel1 <- "How're you?"
		close(channel1)
	}()

	// Using for loop
	for result := range channel1 {
		fmt.Println(result)
	}
}

Output:

chan string, 0xc00009c060
Hello, world!
Hi, friends!
How're you?

Example 2:

// Golang program to demonstrate the
// example of close() function

package main

import (
	"fmt"
)

// Functions for send operation
func fun1(chnl chan string) {
	chnl <- "Hello"
	chnl <- "World"
	chnl <- "How're you?"
	chnl <- "Boys"
	// Closing the channel
	close(chnl)
}

func fun2(chnl chan int) {
	chnl <- 10
	chnl <- 20
	chnl <- 30
	chnl <- 40
	// Closing the channel
	close(chnl)
}

func main() {
	// Creating a channel using make()
	channel1 := make(chan string)
	channel2 := make(chan int)

	// Printing types and values
	fmt.Printf("%T, %v\n", channel1, channel1)
	fmt.Printf("%T, %v\n", channel2, channel2)

	go fun1(channel1)
	go fun2(channel2)

	// Using for loop
	for result := range channel1 {
		fmt.Println(result)
	}
	for result := range channel2 {
		fmt.Println(result)
	}
}

Output:

chan string, 0xc00009c060
chan int, 0xc00009c0c0
Hello
World
How're you?
Boys
10
20
30
40

Golang builtin Package »


ADVERTISEMENT




ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.