So far this is working... // create map var m = make(map[string]net.Conn) // populate map with user connection m[username] = c // get user connection from map conn := m[username] // write to user connection conn.Write([]byte(string(username + " has entered the room\n# ")))
but to handle the map not returning a result ( so I don't attempt to write to a non-existent connection ) I need to know the zero value of type net.Conn does anyone know what this is ?? if only net.Conn was an integer or string this would be simple... or is the only solution to use the comma, ok method when testing the map ? On Sat, 12 Sep 2020 at 18:33, Andy Hall <andyjohnh...@gmail.com> wrote: > > thanks brian...I have looked at sync / channels but I think the > simplicity of mutex should work fine for me. > > On Sat, 12 Sep 2020 at 18:13, Brian Candler <b.cand...@pobox.com> wrote: > > > > On Saturday, 12 September 2020 17:04:19 UTC+1, mb0 wrote: > >> > >> a global map is the way to go, however writing an reading the map from > >> multiple go routines will fail (in your case different handle calls for > >> connections) because the map data structure is not safe for concurrent use > >> and must be coordinated. the way it is usually solved is by adding another > >> sync.Mutex (mutually exclusive) variable to ensure only one go routine at > >> a has access to the map. > > > > > > Another very common pattern is to have a separate goroutine for each > > connection, and then the goroutines communicate using channels. > > > > -- > > You received this message because you are subscribed to a topic in the > > Google Groups "golang-nuts" group. > > To unsubscribe from this topic, visit > > https://groups.google.com/d/topic/golang-nuts/qsMfKWZ8HgI/unsubscribe. > > To unsubscribe from this group and all its topics, send an email to > > golang-nuts+unsubscr...@googlegroups.com. > > To view this discussion on the web visit > > https://groups.google.com/d/msgid/golang-nuts/1cc5595e-d2ef-4dda-ab27-ce2052752393o%40googlegroups.com. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOp5WB5j6wG1x52Ft0Raw1M7RLuo6Si0dmYe9YOK2xS3owcJaA%40mail.gmail.com.