goroutine

How to efficiently close the channel?

白昼怎懂夜的黑 提交于 2019-12-24 18:31:38
问题 I'm trying to do some stuff: type Feed struct { title, descr, link string published time.Time } func main() { ar := make([]Feed, 0) for i := 0; i < 3; i++ { f: = new(Feed) // do some stuff with feed ar = append(ar, *f) } ch := make(chan Feed, 3) for _, i := range ar { go process(i, ch) } r :=0 for i := range ch { fmt.Println(i) r++ if r == 3 { close(ch) } } } func process(i Feed, ch chan Feed) { // do some stuff ch <- i } It seems that ar is unnecessary, but if it would be removed, last range

Deadlock between goroutines

倖福魔咒の 提交于 2019-12-24 08:22:50
问题 I'm new to Go. When I comment out the second goroutine, there is a fatal error. I don't understand what causes this error to occur. Can you explain it to me? package main import ( "fmt" "time" ) func main() { ch := make(chan int) go func() { for i := 0; i < 10; i++ { ch <- i } } () // go func() { for { if num, ok := <-ch; !ok { break } else { fmt.Printf("%d\n", num) } } // } () time.Sleep(2 * time.Second) close(ch) } This prints the following code: 0 1 2 3 4 5 6 7 8 9 fatal error: all

First goroutine example, weird results

Deadly 提交于 2019-12-23 19:46:29
问题 This example taken from tour.golang.org/#63 package main import ( "fmt" "time" ) func say(s string) { for i := 0; i < 5; i++ { time.Sleep(100 * time.Millisecond) fmt.Println(s) } } func main() { go say("world") say("hello") } The output hello world hello world hello world hello world hello Why world is printed only 4 times instead of 5 ? Edit: The answer can be quoted from golang specification: Program execution begins by initializing the main package and then invoking the function main. When

How do I find out if a goroutine is done, without blocking?

。_饼干妹妹 提交于 2019-12-23 08:30:11
问题 All the examples I've seen so far involve blocking to get the result (via the <-chan operator). My current approach involves passing a pointer to a struct: type goresult struct { result resultType; finished bool; } which the goroutine writes upon completion. Then it's a simple matter of checking finished whenever convenient. Do you have better alternatives? What I'm really aiming for is a Qt-style signal-slot system. I have a hunch the solution will look almost trivial ( chan s have lots of

Why does a goroutine have no identity

元气小坏坏 提交于 2019-12-23 05:16:12
问题 I new to golang and I am reading the example from the book gopl. Section 9.8.4 of The Go Programming Language book explains why Goroutines have no notion of identity that is accessible to the programmer Goroutines have no notion of identity that is accessible to the programmer. This is by design, since thread-local storage tends to be abused. For example, in a web server implemented in a language with thread-local storage, it’s common for many functions to find information about the HTTP

Trying to understand goroutines

◇◆丶佛笑我妖孽 提交于 2019-12-22 17:51:22
问题 I've been playing around with the following code from A Tour of Go, but I don't understand what is going on when I apply some minor changes. The original code is this package main import ( "fmt" "time" ) func say(s string) { for i := 0; i < 5; i++ { time.Sleep(100 * time.Millisecond) fmt.Println(s) } } func main() { go say("world") say("hello") } and it produces this world hello hello world world hello hello world world hello which is OK: five times hello, five times world. I starts to get

Trying to understand goroutines

冷暖自知 提交于 2019-12-22 17:50:48
问题 I've been playing around with the following code from A Tour of Go, but I don't understand what is going on when I apply some minor changes. The original code is this package main import ( "fmt" "time" ) func say(s string) { for i := 0; i < 5; i++ { time.Sleep(100 * time.Millisecond) fmt.Println(s) } } func main() { go say("world") say("hello") } and it produces this world hello hello world world hello hello world world hello which is OK: five times hello, five times world. I starts to get

Shared memory vs. Go channel communication

寵の児 提交于 2019-12-22 01:34:00
问题 One of Go's slogans is Do not communicate by sharing memory; instead, share memory by communicating. I am wondering whether Go allows two different Go-compiled binaries running on the same machine to communicate with one another (i.e. client-server), and how fast that would be in comparison to boost::interprocess in C++? All the examples I've seen so far only illustrate communication between same-program routines. A simple Go example (with separate client and sever code) would be much

Parallel processing in golang

无人久伴 提交于 2019-12-21 09:15:01
问题 Given the following code: package main import ( "fmt" "math/rand" "time" ) func main() { for i := 0; i < 3; i++ { go f(i) } // prevent main from exiting immediately var input string fmt.Scanln(&input) } func f(n int) { for i := 0; i < 10; i++ { dowork(n, i) amt := time.Duration(rand.Intn(250)) time.Sleep(time.Millisecond * amt) } } func dowork(goroutine, loopindex int) { // simulate work time.Sleep(time.Second * time.Duration(5)) fmt.Printf("gr[%d]: i=%d\n", goroutine, loopindex) } Can i

On windows, is it possible to run a single goroutine as a different user?

谁都会走 提交于 2019-12-21 05:23:15
问题 How do you delegate the running of a goroutine to another non administrator account on windows? I see you can do this on Linux using syscall.Setuid() . I can't see how to do this on Windows using the windows syscall package. I'd like to be able to set the account the goroutine runs under while the program is running. Is this possible? Bit of background :- I want to switch the user that runs the goroutine so I can change the OS User passed to Oracle during the database connection when I use go