what is a golang channel code example
Example: golang channel
package main
import (
"encoding/json"
"fmt"
"runtime"
"time"
)
type Profile struct {
Name string `json:"name"`
Age uint `json:"age"`
}
func Publisher(data string, channel chan interface{}) {
channel <- string(data)
}
func Subscribe(channel chan interface{}) {
profile := <-channel
fmt.Println(profile)
}
func main() {
runtime.GOMAXPROCS(2)
profileChannel := make(chan interface{})
var profile Profile
profile.Name = "john doe"
profile.Age = 23
toJson, _ := json.Marshal(profile)
go Publisher(string(toJson), profileChannel)
go Subscribe(profileChannel)
time.Sleep(time.Second * 1)
}