Skip to content

Commit 6a7a30f

Browse files
committed
Apply gofmt for example codes
1 parent 23c4ea6 commit 6a7a30f

29 files changed

+96
-110
lines changed

resources/201901/modularization-and-actor/actor/actor.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package actor
22

33
import (
4+
"container/list"
45
"fmt"
56
"reflect"
6-
"container/list"
77
)
88

99
type AID struct {
@@ -15,9 +15,7 @@ func (a *AID) String() string {
1515
return fmt.Sprintf("<%d, %s>", a.ActorID, a.NodeName)
1616
}
1717

18-
1918
type IActorReceiver interface {
20-
2119
GetNodeName() string
2220
}
2321

@@ -34,10 +32,10 @@ var _ IActorReceiver = &Actor{}
3432
var _ iActor = &Actor{}
3533

3634
type Actor struct {
37-
aid *AID
38-
receiver reflect.Value
39-
queue *list.List
40-
inChan chan *ActorCall
35+
aid *AID
36+
receiver reflect.Value
37+
queue *list.List
38+
inChan chan *ActorCall
4139
}
4240

4341
func (a *Actor) start(receiver IActorReceiver, aid *AID) bool {
@@ -70,7 +68,6 @@ func (a *Actor) loop() {
7068
}
7169
}
7270

73-
7471
func (a *Actor) GetNodeName() string {
7572
return a.aid.NodeName
7673
}
@@ -94,8 +91,7 @@ func (a *Actor) process(actorCall *ActorCall) {
9491
}
9592
}
9693

97-
98-
func (a *Actor) makeActorCall(done chan *ActorCall , function interface{}, args ...interface{}) *ActorCall {
94+
func (a *Actor) makeActorCall(done chan *ActorCall, function interface{}, args ...interface{}) *ActorCall {
9995

10096
v := reflect.ValueOf(function)
10197
if v.Kind() != reflect.Func {
@@ -110,7 +106,7 @@ func (a *Actor) makeActorCall(done chan *ActorCall , function interface{}, args
110106
valuedArgs[i+1] = reflect.ValueOf(x)
111107
}
112108

113-
return &ActorCall{Function:v, Args:valuedArgs, Done:done}
109+
return &ActorCall{Function: v, Args: valuedArgs, Done: done}
114110

115111
}
116112

@@ -119,4 +115,4 @@ func (a *Actor) call(function interface{}, args ...interface{}) ([]interface{},
119115
a.inChan <- a.makeActorCall(done, function, args...)
120116
actorCall, _ := <-done
121117
return actorCall.GetResults()
122-
}
118+
}

resources/201901/modularization-and-actor/actor/actor_system.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package actor
22

33
import (
44
"context"
5-
"sync"
6-
"sync/atomic"
75
"fmt"
8-
"runtime"
6+
"net/http"
97
"reflect"
8+
"runtime"
109
"strings"
11-
"net/http"
10+
"sync"
11+
"sync/atomic"
1212
)
1313

1414
type ActorProtocolType uint16
@@ -26,8 +26,8 @@ type Request struct {
2626
}
2727

2828
type Response struct {
29-
Err error
30-
Results []interface{}
29+
Err error
30+
Results []interface{}
3131
}
3232

3333
var defaultActorSystem *ActorSystem
@@ -37,20 +37,20 @@ func init() {
3737
}
3838

3939
type ActorSystem struct {
40-
mutex sync.RWMutex
41-
nowActorID uint64
42-
actorMapByAID map[*AID]iActor
43-
actorMapByNodeName map[string]iActor
40+
mutex sync.RWMutex
41+
nowActorID uint64
42+
actorMapByAID map[*AID]iActor
43+
actorMapByNodeName map[string]iActor
4444

45-
server *http.Server
46-
serverCloseFunc context.CancelFunc
45+
server *http.Server
46+
serverCloseFunc context.CancelFunc
4747
}
4848

4949
func newActorSystem() *ActorSystem {
5050
d := &ActorSystem{
51-
actorMapByAID: make(map[*AID]iActor),
52-
actorMapByNodeName: make(map[string]iActor),
53-
nowActorID: 0,
51+
actorMapByAID: make(map[*AID]iActor),
52+
actorMapByNodeName: make(map[string]iActor),
53+
nowActorID: 0,
5454
}
5555
return d
5656
}
@@ -87,7 +87,7 @@ func (as *ActorSystem) startWebActor(receiver IActorReceiver, nodeName string, a
8787

8888
func (as *ActorSystem) createAID(nodeName string) *AID {
8989
id := atomic.AddUint64(&as.nowActorID, 1)
90-
return &AID{NodeName:nodeName, ActorID:id}
90+
return &AID{NodeName: nodeName, ActorID: id}
9191
}
9292

9393
func (as *ActorSystem) getActorByAIDWithLock(aid *AID) (iActor, error) {
@@ -114,8 +114,7 @@ func (d *ActorSystem) getActorByNodeNameWithLock(nodeName string) (iActor, error
114114

115115
func (as *ActorSystem) createRequest(ac iActor, function interface{}, args ...interface{}) *Request {
116116

117-
118-
req := &Request{NodeName:ac.GetNodeName()}
117+
req := &Request{NodeName: ac.GetNodeName()}
119118

120119
v := reflect.ValueOf(function)
121120
if v.Kind() == reflect.Func {
@@ -156,4 +155,4 @@ func (as *ActorSystem) findFunc(ac iActor, funcName string) (interface{}, error)
156155
}
157156
fn := method.Func.Interface()
158157
return fn, nil
159-
}
158+
}

resources/201901/modularization-and-actor/actor/actorcall.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package actor
33
import "reflect"
44

55
type ActorCall struct {
6-
Function reflect.Value
7-
Args []reflect.Value
8-
Results []reflect.Value
9-
Done chan *ActorCall
10-
Error error
6+
Function reflect.Value
7+
Args []reflect.Value
8+
Results []reflect.Value
9+
Done chan *ActorCall
10+
Error error
1111
}
1212

1313
func (c ActorCall) GetResults() ([]interface{}, error) {
@@ -20,11 +20,11 @@ func (c ActorCall) GetResults() ([]interface{}, error) {
2020
var err error
2121
for _, x := range values {
2222
itf := x.Interface()
23-
errorType := reflect.TypeOf((*error)(nil)).Elem()
23+
errorType := reflect.TypeOf((*error)(nil)).Elem()
2424

2525
switch x.Type() {
2626
case errorType:
27-
if castingErr, ok := itf.(error); ok && castingErr != nil {
27+
if castingErr, ok := itf.(error); ok && castingErr != nil {
2828
err = x.Interface().(error)
2929
}
3030
default:

resources/201901/modularization-and-actor/actor/golobal.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package actor
22

33
import (
4-
"fmt"
5-
"net/http"
4+
"bytes"
65
"context"
76
"encoding/gob"
8-
"bytes"
7+
"fmt"
8+
"net/http"
99
)
1010

1111
func StartActor(receiver IActorReceiver) *AID {
@@ -70,4 +70,4 @@ func httpCall(w http.ResponseWriter, req *http.Request) {
7070
enc := gob.NewEncoder(&buffer)
7171
enc.Encode(response)
7272
w.Write(buffer.Bytes())
73-
}
73+
}

resources/201901/modularization-and-actor/actor/web_actor.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,29 @@ package actor
33
import (
44
"bytes"
55
"encoding/gob"
6-
"net/http"
76
"fmt"
7+
"net/http"
88
"reflect"
99
)
1010

1111
var _ IActorReceiver = &Actor{}
1212
var _ iActor = &Actor{}
1313

1414
type WebActor struct {
15-
aid *AID
16-
receiver reflect.Value
17-
shutdownCh chan bool
15+
aid *AID
16+
receiver reflect.Value
17+
shutdownCh chan bool
1818

19-
address string
20-
client *http.Client
19+
address string
20+
client *http.Client
2121
}
2222

23-
2423
func (a *WebActor) start(receiver IActorReceiver, aid *AID) bool {
2524
a.aid = aid
2625
a.receiver = reflect.ValueOf(receiver)
2726
return true
2827
}
2928

30-
3129
func (a *WebActor) GetNodeName() string {
3230
return a.aid.NodeName
3331
}

resources/201901/modularization-and-actor/main_sample.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"sampleactor/samplestruct"
4+
"sampleactor/samplestruct"
55
)
66

77
func main() {

resources/201901/modularization-and-actor/main_sample_local_actor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ func main() {
2222

2323
//SetLevel(level int)
2424
_, _ = actor.Call(heroAID, (*samplestruct.Hero).SetLevel, results[0])
25-
}
25+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package main
22

33
import (
4-
"sampleactor/actor"
54
"context"
5+
"sampleactor/actor"
66
"sampleactor/samplestruct"
77
)
88

99
func main() {
1010

1111
ctxMain, _ := context.WithCancel(context.Background())
1212

13-
actor.StartWebServer(ctxMain,"9999")
13+
actor.StartWebServer(ctxMain, "9999")
1414

1515
training := samplestruct.NewTraining("Training")
1616

@@ -21,4 +21,4 @@ func main() {
2121
case <-ctxMain.Done():
2222
}
2323
}
24-
}
24+
}

resources/201901/modularization-and-actor/samplestruct/hero.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ package samplestruct
33
import "fmt"
44

55
type Hero struct {
6-
id int
7-
level int
6+
id int
7+
level int
88
nodeName string
99
}
1010

1111
func NewHero(id int, level int, nodeName string) *Hero {
1212
return &Hero{
13-
id: id,
14-
level: level,
13+
id: id,
14+
level: level,
1515
nodeName: nodeName,
1616
}
1717
}
@@ -27,4 +27,4 @@ func (h *Hero) SetLevel(level int) {
2727

2828
func (h *Hero) GetNodeName() string {
2929
return h.nodeName
30-
}
30+
}

resources/201901/modularization-and-actor/samplestruct/training.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func (t *Training) TrainingHero(hero *Hero) {
1919
}
2020

2121
func (t *Training) Do(level int) int {
22-
newLevel := level+1
22+
newLevel := level + 1
2323
fmt.Printf("Training.Do: oldLevel %d, nowLevel %d \n", level, newLevel)
2424
return newLevel
2525
}

resources/201903/codelab/2_1_cross_compile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ func main() {
77
}
88

99
// GOOS=linux GOARCH=amd64 go build 4_1_cross_compile.go
10-
// GOOS=windows GOARCH=amd64 go build 4_1_cross_compile.go
10+
// GOOS=windows GOARCH=amd64 go build 4_1_cross_compile.go

resources/201903/codelab/3_1_helloworld.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package main
22

33
import "fmt"
44

5-
func main(){
5+
func main() {
66
fmt.Println("Hello world.")
77
}
8-
9-

resources/201903/codelab/3_2_export.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package main
22

33
import "fmt"
44

5-
func main(){
5+
func main() {
66
fmt.println("Hello world.")
77
}
8-
9-
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package main
22

33
import (
4-
t "text/template"
54
h "html/template"
5+
t "text/template"
66
)
77

88
func main() {
99
t.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)
1010
h.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)
11-
}
11+
}

resources/201903/codelab/3_4_blank.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
func main() {
1111
db, err := sql.Open("mysql", "user:password@/database")
1212
if err != nil {
13-
panic(err.Error()) // Just for example purpose. You should use proper error handling instead of panic
13+
panic(err.Error()) // Just for example purpose. You should use proper error handling instead of panic
1414
}
1515
defer db.Close()
1616

1717
fmt.Println(db.Ping())
18-
}
18+
}

resources/201903/codelab/4_11_array.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package main
22

33
import "fmt"
44

5-
func main(){
6-
var a [4]int
7-
b := [2]string{"Penn", "Teller"}
8-
c := [...]bool{true, false, false}
5+
func main() {
6+
var a [4]int
7+
b := [2]string{"Penn", "Teller"}
8+
c := [...]bool{true, false, false}
99

10-
fmt.Println(a,b,c)
10+
fmt.Println(a, b, c)
1111
}

0 commit comments

Comments
 (0)