Skip to content

Commit f1af231

Browse files
committed
code review
1 parent 98b48a6 commit f1af231

File tree

5 files changed

+31
-14
lines changed

5 files changed

+31
-14
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func main() {
3131
wg.Add(1)
3232
go func() {
3333
defer wg.Done()
34-
goroutineId, err = goroutine_id.GetGoroutineID()
34+
goroutineId, err = goroutine_id.GetGoroutineId()
3535
if err != nil {
3636
panic(err)
3737
}
@@ -42,7 +42,7 @@ func main() {
4242
// Goroutine ID: 6
4343

4444
// Look at the id of the main coroutine
45-
goroutineId, err = goroutine_id.GetGoroutineID()
45+
goroutineId, err = goroutine_id.GetGoroutineId()
4646
if err != nil {
4747
panic(err)
4848
}

errors.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package goroutine_id
2+
3+
import "errors"
4+
5+
var (
6+
// ErrGetGoroutineIdFailed 获取goroutine id失败
7+
ErrGetGoroutineIdFailed = errors.New("get goroutine id failed")
8+
)

examples/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func main() {
1616
wg.Add(1)
1717
go func() {
1818
defer wg.Done()
19-
goroutineId, err = goroutine_id.GetGoroutineID()
19+
goroutineId, err = goroutine_id.GetGoroutineId()
2020
if err != nil {
2121
panic(err)
2222
}
@@ -27,7 +27,7 @@ func main() {
2727
// Goroutine ID: 6
2828

2929
// Look at the id of the main coroutine
30-
goroutineId, err = goroutine_id.GetGoroutineID()
30+
goroutineId, err = goroutine_id.GetGoroutineId()
3131
if err != nil {
3232
panic(err)
3333
}

goroutine_id_stack_based.go

+18-9
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,30 @@ import (
99

1010
// ------------------------------------------------- --------------------------------------------------------------------
1111

12-
// GetGoroutineID 获取当前协程的ID
13-
func GetGoroutineID() (int, error) {
14-
id, err := strconv.Atoi(GetGoroutineIDAsString())
12+
// GetGoroutineId 获取当前协程的id
13+
func GetGoroutineId() (int, error) {
14+
15+
goroutineIdString := GetGoroutineIdAsString()
16+
if goroutineIdString == "" {
17+
return 0, ErrGetGoroutineIdFailed
18+
}
19+
20+
goroutineId, err := strconv.Atoi(goroutineIdString)
1521
if err != nil {
16-
return 0, fmt.Errorf("cannot get goroutine id: %v", err)
22+
return 0, fmt.Errorf("goroutine goroutineId parse int error, value = %s, msg = %s", goroutineIdString, err.Error())
1723
}
18-
return id, nil
24+
return goroutineId, nil
1925
}
2026

21-
// GetGoroutineIDAsString 获取当前协程的ID,以字符串的形式返回
22-
func GetGoroutineIDAsString() string {
27+
// GetGoroutineIdAsString 获取当前协程的id,以字符串的形式返回
28+
func GetGoroutineIdAsString() string {
2329
var buf [64]byte
2430
n := runtime.Stack(buf[:], false)
25-
idField := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine "))[0]
26-
return idField
31+
fields := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine "))
32+
if len(fields) < 1 {
33+
return ""
34+
}
35+
return fields[0]
2736
}
2837

2938
// ------------------------------------------------- --------------------------------------------------------------------

goroutine_id_stack_based_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestGetGoroutineID(t *testing.T) {
1414
wg.Add(1)
1515
go func() {
1616
defer wg.Done()
17-
goroutineId, _ = GetGoroutineID()
17+
goroutineId, _ = GetGoroutineId()
1818
}()
1919
wg.Wait()
2020

0 commit comments

Comments
 (0)