create a string channel in go

To create a string channel in Go, you can use the built-in function make to make a new channel and specify the type of the channel to be string. You can then use this channel to send and receive strings between goroutines.

Here's an example of how to create a string channel:

main.go
strChan := make(chan string)
29 chars
2 lines

In this example, strChan is a string channel that allows you to send and receive strings.

You can then use this channel to communicate between goroutines. For example, to send a string on the strChan channel, you can use the following code:

main.go
strChan <- "hello world"
25 chars
2 lines

And to receive a string from the strChan channel, you can use the following code:

main.go
msg := <- strChan
18 chars
2 lines

This code will block until a value is available on the strChan channel, and then assign the received string to the msg variable.

gistlibby LogSnag