channel

Why Go's channel can close twice?

人走茶凉 提交于 2019-12-21 06:34:32
问题 When I am do some go practices code, I encounter a problem that a channel can be closed twice like this: // jobs.go package main import ( "fmt" ) func main() { fmt.Println("Hello, playground") jobs := make(chan int, 5) done := make(chan bool) go func() { for { j,more := <-jobs fmt.Println("receive close: ", j, more) done <- true } }() close(jobs) <- done } Output: ~ go run jobs.go Hello, playground receive close: 0 false receive close: 0 false But when I close the channel twice manually, I

Why Go's channel can close twice?

妖精的绣舞 提交于 2019-12-21 06:33:56
问题 When I am do some go practices code, I encounter a problem that a channel can be closed twice like this: // jobs.go package main import ( "fmt" ) func main() { fmt.Println("Hello, playground") jobs := make(chan int, 5) done := make(chan bool) go func() { for { j,more := <-jobs fmt.Println("receive close: ", j, more) done <- true } }() close(jobs) <- done } Output: ~ go run jobs.go Hello, playground receive close: 0 false receive close: 0 false But when I close the channel twice manually, I

Using channel or sync.Cond to wait for condition

旧街凉风 提交于 2019-12-21 05:47:10
问题 I am trying to wait for a specific condition, and I would like advice as to how this is done best. I have a struct that looks like this (simplified): type view struct { timeFrameReached bool Rows []*sitRow } In a goroutine, I am updating a file, which is read into the view variable. The number of rows increases, and timeFrameReached will ultimately be true . Elsewhere, I want to wait for the following condition to be true: view.timeFrameReached == true || len(view.Rows) >= numRows I am trying

Sending messages to multiple rooms using Socket.io?

你说的曾经没有我的故事 提交于 2019-12-21 05:47:07
问题 Is it possible to send messages to multiple rooms using socket.io? Sending to 1 room: io.sockets.in(room).emit("id", {}) Sending to N rooms: io.sockets.in(room1, room2, roomN).emit("id", {}) 回答1: The sockets.in method only accepts one room as an argument, so to broadcast to multiple rooms you would have to reset the room, in between emissions. Something like this should work: ['room1', 'room2', 'room3'].forEach(function(room){ io.sockets.in(room).emit("id", {}); }); 回答2: Yes, it's possible to

Pseudo Tcp channel

孤街浪徒 提交于 2019-12-21 05:10:32
问题 What is a pseudo-tcp channel and how can it be implemented? 回答1: Pseudo-TCP is a protocol that implements some of the ideas of TCP to provide a reliable data stream over an unreliable, packet-based interface. This could be used, for example, if you had access to only UDP but wanted a reliable way to pass data. You can find example code here: Google Code - PseudoTCP Channel (Header File) Google Code - PseudoTCP Channel (CC File) 来源: https://stackoverflow.com/questions/5570663/pseudo-tcp

Is it possible to multiplex several channels into one?

空扰寡人 提交于 2019-12-21 03:47:14
问题 The idea is to have a variable number of channels in a slice, push each value received through them into a single channel, and close this output channel once the last one of the input channels is closed. Something like this, but for a number of channels more than two: func multiplex(cin1, cin2, cout chan int) { n := 2 for { select { case v, ok := <-cin1: if ok { cout <- v } else { n -= 1 } case v, ok := <-cin2: if ok { cout <- v } else { n -= 1 } } if n == 0 { close(cout) break } } } The

Struct variable not being updated

点点圈 提交于 2019-12-20 06:34:40
问题 I have an array in test code arr := []Server{} which asks for arr[0].GetId() Server is an interface. ServerInstance is a struct implementing a method of interface, i.e func (serv ServerInstance) GetId() int { return serv.Id } I have a goroutine like func (serv *ServerInstance) someFunc which is updating a variable 'Id' of struct. I am sure of value being updated as - serv.Id=23445 But this is not being reflected in call at line 3 *Update*** for somecondition { arr=append(arr

Is there any way to make Go's channels behave like a stack

跟風遠走 提交于 2019-12-19 09:27:25
问题 Go channels by default behave like a queue as far as I can tell, first in first out. Is there any way to change them to work last in first out? Basically I am doing a search and want to do DFS instead of BFS for memory constraints. 回答1: No, this is not possible - channels are always FIFO. You could use package container/heap. 来源: https://stackoverflow.com/questions/17755222/is-there-any-way-to-make-gos-channels-behave-like-a-stack

Thread interrupt not ending blocking call on input stream read

痞子三分冷 提交于 2019-12-18 21:17:27
问题 I'm using RXTX to read data from a serial port. The reading is done within a thread spawned in the following manner: CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(port); CommPort comm = portIdentifier.open("Whatever", 2000); SerialPort serial = (SerialPort)comm; ...settings Thread t = new Thread(new SerialReader(serial.getInputStream())); t.start(); The SerialReader class implements Runnable and just loops indefinitely, reading from the port and constructing the

How does make(chan bool) behave differently from make(chan bool, 1)?

∥☆過路亽.° 提交于 2019-12-18 13:07:29
问题 My question arises from trying to read a channel, if I can, or write it, if I can, using a select statement. I know that channels specified like make(chan bool, 1) are buffered, and part of my question is what is the difference between that, and make(chan bool) -- which this page says is the same thing as make(chan bool, 0) --- what is the point of a channel that can fit 0 values in it? See playground A : chanFoo := make(chan bool) for i := 0; i < 5; i++ { select { case <-chanFoo: fmt.Println