-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.go
617 lines (468 loc) · 7.86 KB
/
go.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import fmt
func func_name() {
}
func GetName() (firstName, lastName, nickName string) {
return "M", "R", "A"
}
_, _, nickName := GetName()
var v1 int = 10
var v2 = 10
v3 := 10
i, j = j, i
const Pi float64 = 3.141592653
const zero = 0.0
const (
size int64 = 1024
eof = -1
)
const u, v float32 = 0, 3
const mask = 1 << 3
const Home = os.GetEnv("HOME")
const ( // iota = 0
c0 = iota // c0 = 0
c1 = iota // c1 = 1
c2 = iota // c2 = 2
)
const (
a = 1 << iota // a = 1
b = 1 << iota // b = 2
c = 1 << iota // c = 4
)
const X = iota //0
const Y = iota //0
var c rune = 'a'
var p int64 = 3423
var v1 bool = true
var v2 bool = false
v3 := (1 == 2)
var b1 uint32 = 34234
var value1 int64
value3 := 64
//value1 = value3 error!
value1 = int64(value3)
i, j = 1, 2
if i == j {
fmt.Println("equal")
}
import "math"
func IsEqual(f1, f2, p float64) bool {
return math.Abs(f1-f2) < p
}
var str string
str = "Hello World"
ch := str[0]
str := "Hello"
n := len(str)
for i := 0; i < n; i++ {
ch := str[i]
fmt.Println(i, ch)
}
[2*N] struct { x, y int32 }
[1000]*float64
arrLength := len(arr)
for i := 0; i < len(arr); i++ {
fmt.Println()
}
for i, v := range arr {
}
package main
import "fmt"
func main() {
var myArray [10]int = [10]int{1, 2, 3}
var mySlice []int = myArray[:5]
mySlice = myArray[:]
}
mySlice := make([]int, 5, 10)
for i, v := range mySlice {
}
fmt.Println(cap(mySlice))
mySlice = append(mySlice, 1, 2, 3)
mySlice = append(mySlice, mySlice2...)
oldSlice := []int{1, 2, 3}
newSlice := oldSlice[:3]
type PersonInfo struct {
ID string
Name string
Address string
}
func main() {
var personDB map[string] PersonInfo
personDB = make(map[string] PersonInfo)
personDB["123"] = PersonInfo{"123", "T", "R"}
personDB["1"] = PersonInfo{"1", "J", "R"}
person, ok := personDB["1234"]
if ok {
fmt.Println("F", person.Name, "1123")
} else {
fmt.Println("Didnt find 1123")
}
}
var myMap map[string] PersonInfo
myMap = make(map[string] PersonInfo, 100)
myMap = map[string] PersonInfo {
"123": PersonInfo {"1", "J", "R"},
}
delete(myMap, "1234")
value, ok := myMap["1234"]
if ok {
}
if a < 5 {
return 0
} else {
return 1
}
switch i {
case 0:
fmt.Println("0")
default:
fmt.Println("default")
}
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
sum := 0
for {
sum++
if sum > 100 {
break
}
}
a := []int{1, 2, 3}
for i, j := 0, len(a) - 1; i < j; i, j = i + 1, j -1 {
a[i], a[j] = a[j], a[i]
}
for j := 0; j < 5; j++ {
for i := 0; i < 10; i++ {
if i > 5 {
break JLoop
}
}
}
JLoop:
func myfunc() {
i := 0
HERE:
fmt.Println(i)
i++
if i < 10 {
goto HERE
}
}
package mymath
import "errors"
func Add(a int, b int) (ret int, err error) {
if a < 0 || b < 0 {
err = error.New("ass")
return
}
return a + b, nil
}
import "mymath"
c := mymath.Add(1, 2)
func myfunc(args ...int) {
for _, arg := range args {
fmt.Println(arg)
}
}
myfunc(2, 3, 4)
myfunc(1, 3, 7, 13)
func myfunc(arg ...int) {
myfunc(arg...)
myfunc(arg[1:]...)
}
func Printf(format string, args ...interface{}) {
}
package main
import "fmt"
func MyPrintf(arg ...interface{}) {
for _, arg := range args {
switch arg.(type) {
case int:
fmt.Println(arg, "ss")
case string:
fmt.Println(arg, "ff")
case int64:
fmt.Println(arg, "fa")
}
}
}
func main() {
var v1 int = 1
var v2 int64 = 234
var v3 string = "hello"
var v4 float32 = 1.123
MyPrintf(v1, v2, v3, v4)
}
n, _ := f.Read(buf)
f := func(a, b int, z float64) bool {
return a * b < int(z)
}
func(ch chan int) {
ch <- ACK
} (reply_chan)
package main
import (
"fmt"
)
func main() {
var j int = 5
a := func() (func()) {
var i int = 10
return func() {
fmt.Println(i, j)
}
}()
a()
j *= 2
a()
}
type error interface {
Error() string
}
func Foo(param int) (n int, err error) {
}
n, err := Foo(0)
if err != nil {
} else {
}
if a < 5 {
} else {
}
type PathError struct {
Op string
Path string
Err error
}
func (e *PathError) Error() string {
}
func Stat(name string) (fi FileInfo, err error) {
var stat syscall.Stat_t
err = syscall.Stat(name, &stat)
if err != nil {
return nil,
}
return
}
type Integer int
func (a Integer) Less(b Integer) bool {
return a < b
}
func main() {
var a Integer = 1
if a.Less(2) {
fmt.Println(a, "Less 2")
}
}
func (a *Integer) Add(b Integer) {
*a += b
}
func main() {
var a Integer = 1
a.Add(2)
fmt.Println("a =", a) // a = 3
}
a := 1
func (a Integer) Add(b Integer) {
a += b
}
a.Add(2) // a = 1
type Header map[string][]string
func (h Header) Add(key, value string) {
textproto.MIMEHeader(h).Add(key, value)
}
func (h Header) Set(key, value string) {
textproto.MIMEHeader(h).Set(key, value)
}
b = a
b.Modeify()
type Rect struct {
x, y float64
width, heigth float64
}
func (r *Rect) Area() float64 {
return r.width * r.heigth
}
rect1 := new(Rect)
rect2 := &Rect{}
rect3 := &Rect{0, 0, 100, 200}
rect4 := &Rect{width: 100, heigth: 200}
func NewRect(x, y, width, heigth float64) *Rect {
return &Rect{x, y, width, heigth}
}
type Base struct {
Name string
}
func (base *Base) Foo() {
}
func (base *Base) Bar() {
}
type Foo struct {
Base
}
func (foo * Foo) Bar() {
foo.Base.Bar
}
type Foo struct {
*Base
}
type Job struct {
Command string
*log.Logger
}
func (job *Job) Start() {
job.Log("start")
job.Log("end")
}
type Rect struct {
X, Y float64
Width, Height float64
}
func (r *Rect) area() float64 {
return r.Width * r.Height
}
goroutine
channel
type File struct {
}
func (f *File) Read(buf []byte) (n int, err error)
func (f *File) Write(buf []byte) (n int, err error)
func (f *File) Seek(buf []byte) (n int, err error)
func (f *File) Close(buf []byte) error
type IFile interface {
Read(buf []byte) (n int, err error)
Write(buf []byte) (n int, err error)
Seek(off int64, whence int) (pos int64, err error)
Close() error
}
type IReader interface {
Read(buf []byte) (n int, err error)
}
type IWriter interface {
Write(buf []byte) (n int, err error)
}
type ICloser interface {
Close() error
}
var file1 IFile = new(File)
var file2 IReader = new(File)
var file3 IWriter = new(File)
var file4 ICloser = new(File)
var v1 interface{} =
switch v := v1.(type) {
}
func Add(x, y int) {
z := x + y
fmt.Println(z)
}
go Add(1, 1)
func main() {
for i := 0; i < 10; i++ {
go Add(i, i)
}
}
func Count(lock *sync.Mutex) {
lock.Lock()
counter++
fmt.Println(counter)
lock.Unlock()
}
func main() {
lock := &sync.Mutex{}
for i := 0; i < 10; i++ {
go Count(lock)
}
for {
lock.Lock()
c := counter
lock.Unlock()
runtime.Gosched()
if c >= 10 {
break
}
}
}
package main
import "fmt"
func Count(ch chan int) {
fmt.Println("counting")
ch <- 1
}
func main() {
chs := make([]chan int, 10)
for i := 0; i < 10; i++ {
chs[i] = make(chan int)
go Count(chs[i])
}
for _, ch := range(chs) {
<-ch
}
}
var chanName chan ElementType
var ch chan int //生命
var m map[string] chan bool
ch := make(chan int) //定义
ch <- value
value := <-ch
ch := make(chan int, 1)
for {
select {
case ch <- 0 {
}
case ch <- 1 {
}
}
i := <-ch
}
c := make(chan int, 1024)
for i := range c {
fmt.Println("")
}
i := <-ch
timeout := make(chan bool, 1)
go func {
time.Sleep(9)
timeout <- true
}()
select {
case <- ch {
}
case <- timeout {
}
}
type PipeData struct {
value int
handler fun(int) int
next chan int
}
func handle(queue chan *PipeData) {
for data := range queue {
data.next <- data.handler(data.value)
}
}
var ch2 chan<- float64
var ch3 <-chan int
ch4 := make(chan int)
ch5 := <-chan int(ch4)
ch6 := chan<- int(ch4)
func Parse(ch <- chan int) {
for value := range ch {
fmt.Println("Parse", value)
}
}
close(ch)
x, ok := <-ch
type Vector []float64
func (v Vector)DoSome(i, n int, u Vector, c chan int) {
for ; i < n; i++ {
v[i] += u.Op(v[i])
}
c <- 1
}
const NCPU = 16
func (v Vector)DoAll(u Vector) {
c := make(chan int, NCPU)
for i := 0; i < NCPU; i++ {
go v.DoSome(i*len(v)/NCPU, (i+1)*len(v)/NCPU, u, c)
}
}