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.go29 chars2 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.go25 chars2 lines
And to receive a string from the strChan
channel, you can use the following code:
main.go18 chars2 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