The better error handling suggestion for a concurrent execution using go routine.
Explore the docs »
View Demo
Table of Contents
Static analysis is a great tool to find problems often related to performance, coding style, and some logic errors without running the application. I have implemented a tool that suggests to use sync.ErrGroup instead of sync.WaitGroup as sync.ErrGroup is generally more useful when you wish to handle error for groups of goroutines working on subtasks of a common task in parallel.
When you run the test from error_handling_test.go, it prints out the following AST. Then, analyze the following AST tree that is comprised of AST nodes.
88 . . . . . 1: *ast.GoStmt { <- AST node for Go Routine
89 . . . . . . Go: sample.go:11:2
90 . . . . . . Call: *ast.CallExpr {
91 . . . . . . . Fun: *ast.FuncLit { <- AST node for function literal
92 . . . . . . . . Type: *ast.FuncType {
93 . . . . . . . . . Func: sample.go:11:5
94 . . . . . . . . . Params: *ast.FieldList { <- AST node for field list
95 . . . . . . . . . . Opening: sample.go:11:9
96 . . . . . . . . . . List: []*ast.Field (len = 1) { <- contains one param
97 . . . . . . . . . . . 0: *ast.Field {
98 . . . . . . . . . . . . Names: []*ast.Ident (len = 1) {
99 . . . . . . . . . . . . . 0: *ast.Ident {
100 . . . . . . . . . . . . . . NamePos: sample.go:11:10
101 . . . . . . . . . . . . . . Name: "ch"
102 . . . . . . . . . . . . . . Obj: *ast.Object {
103 . . . . . . . . . . . . . . . Kind: var
104 . . . . . . . . . . . . . . . Name: "ch"
105 . . . . . . . . . . . . . . . Decl: *(obj @ 97)
106 . . . . . . . . . . . . . . }
107 . . . . . . . . . . . . . }
108 . . . . . . . . . . . . }
109 . . . . . . . . . . . . Type: *ast.ChanType {
110 . . . . . . . . . . . . . Begin: sample.go:11:13
111 . . . . . . . . . . . . . Arrow: - <- We care about only this arrow type for now.
112 . . . . . . . . . . . . . Dir: 3
113 . . . . . . . . . . . . . Value: *ast.Ident {
114 . . . . . . . . . . . . . . NamePos: sample.go:11:18
115 . . . . . . . . . . . . . . Name: "error" <- if it uses error then we can confirm that this goroutine func is used for error handling
116 . . . . . . . . . . . . . }
117 . . . . . . . . . . . . }
118 . . . . . . . . . . . }
119 . . . . . . . . . . }
120 . . . . . . . . . . Closing: sample.go:11:23
121 . . . . . . . . . }
122 . . . . . . . . }Distributed under the MIT License. See LICENSE.txt for more information.
Daiki Kubo - LinkedIn
Project Link: https://github.com/daikidev111/static_analysis