10 examples of 'goroutine' in Go

Every line of 'goroutine' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
44func goroutine(name string, share chan int) {
45 for {
46
47 // Wait to receive a value.
48 value, ok := <-share
49 if !ok {
50
51 // If the channel was closed, return.
52 fmt.Printf("Goroutine %s Down\n", name)
53 return
54 }
55
56 // Display the value.
57 fmt.Printf("Goroutine %s Inc %d\n", name, value)
58
59 // Terminate when the value is 10.
60 if value == 10 {
61 close(share)
62 fmt.Printf("Goroutine %s Down\n", name)
63 return
64 }
65
66 // Increment the value and send it
67 // over the channel.
68 share <- (value + 1)
69 }
70}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

69func (p *Pool) Go(goroutine func(stop chan bool)) {
70 p.lock.Lock()
71 newRoutine := routine{
72 goroutine: goroutine,
73 stop: make(chan bool, 1),
74 }
75 p.routines = append(p.routines, newRoutine)
76 p.waitGroup.Add(1)
77 Go(func() {
78 goroutine(newRoutine.stop)
79 p.waitGroup.Done()
80 })
81 p.lock.Unlock()
82}
176func (c *appClient) retainGoroutine() bool {
177 c.mtx.Lock()
178 defer c.mtx.Unlock()
179 if c.hasQuit() {
180 return false
181 }
182 c.backgroundWait.Add(1)
183 return true
184}
94func goRoutineClosure() {
95
96 const N = 10
97 m := make(map[int]int)
98
99 wg := &sync.WaitGroup{}
100 mu := &sync.Mutex{}
101 wg.Add(N)
102 for i := 0; i < N; i++ {
103 go func(i int) {
104 defer wg.Done()
105 mu.Lock()
106 m[i] = i
107 mu.Unlock()
108 }(i)
109 }
110 wg.Wait()
111 println(len(m))
112}
29func doSomeJob(numRoutines int) {
30 for {
31 for i := 0; i < numRoutines; i++ {
32 go allocateAndSum(rand.Intn(1024) * 1024)
33 }
34 log.Printf("All %d routines started\n", numRoutines)
35 time.Sleep(1000 * time.Millisecond)
36 runtime.GC()
37 }
38}
52func (s *PoolScraper) mainLoop(rateType string) {
53 for {
54 select {
55
56 case <-s.shutdown: // user requested shutdown
57 log.Println("PoolScraper shutting down")
58 s.cleanup(nil)
59 return
60 }
61 }
62}
833func (nc *Conn) spinUpGoRoutines() {
834 // Make sure everything has exited.
835 nc.waitForExits()
836
837 // We will wait on both.
838 nc.wg.Add(2)
839
840 // Spin up the readLoop and the socket flusher.
841 go nc.readLoop()
842 go nc.flusher()
843
844 nc.mu.Lock()
845 if nc.Opts.PingInterval > 0 {
846 if nc.ptmr == nil {
847 nc.ptmr = time.AfterFunc(nc.Opts.PingInterval, nc.processPingTimer)
848 } else {
849 nc.ptmr.Reset(nc.Opts.PingInterval)
850 }
851 }
852 nc.mu.Unlock()
853}
69func (self *CountdownTimer) Wait(routine string) {
70 // 保存指针
71 if _, ok := self.Routines[routine]; !ok {
72 return
73 }
74 rt := self.Routines[routine]
75
76 self.Flag[routine] = make(chan bool)
77 defer func() {
78 if err := recover(); err != nil {
79 logs.Log.Error("动态倒计时器: %v", err)
80 }
81 select {
82 case <-self.Flag[routine]:
83 n := rt.Curr / 1.2
84 if n > rt.Min {
85 rt.Curr = n
86 } else {
87 // 等待时间不能小于设定时间
88 rt.Curr = rt.Min
89 }
90
91 if rt.Curr < self.Level[0] {
92 // 等待时间不能小于最小水平
93 rt.Curr = self.Level[0]
94 }
95 default:
96 rt.Curr = rt.Curr * 1.2
97 if rt.Curr > self.Level[len(self.Level)-1] {
98 // 等待时间不能大于最大水平
99 rt.Curr = self.Level[len(self.Level)-1]
100 }
101 }
102 }()
103 for k, v := range self.Level {
104 if v < rt.Curr {
105 continue
106 }
107
108 if k != 0 && v != rt.Curr {
109 k--
110 }
111 logs.Log.Critical("************************ ……<%s> 倒计时等待 %v 分钟……************************", routine, self.Level[k])
112 time.Sleep(time.Duration(self.Level[k]) * time.Minute)
113 break
114 }
115 close(self.Flag[routine])
116}
82func (s *BinanceScraper) mainLoop() {
83 close(s.initDone)
84 for {
85 select {
86 case <-s.shutdown: // user requested shutdown
87 log.Println("BinanceScraper shutting down")
88 s.cleanup(nil)
89 return
90 }
91 }
92}
229func (wnd *Window) MainLoop(run func()) {
230 // main loop
231 for !wnd.close {
232 err := wnd.StartFrame()
233 if err != nil {
234 time.Sleep(10 * time.Millisecond)
235 continue
236 }
237
238 for _, event := range wnd.events {
239 switch e := event.(type) {
240 case *sdl.WindowEvent:
241 if e.Event == sdl.WINDOWEVENT_CLOSE {
242 wnd.close = true
243 }
244 case *sdl.QuitEvent:
245 wnd.close = true
246 case *sdl.KeyboardEvent:
247 if e.Type == sdl.KEYDOWN && e.Keysym.Scancode == sdl.SCANCODE_ESCAPE {
248 wnd.close = true
249 }
250 }
251 }
252
253 run()
254
255 wnd.FinishFrame()
256 }
257}

Related snippets