You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -64,28 +64,17 @@ Identifiers name program entities such as variables and types. An identifier is
64
64
## Keywords
65
65
66
66
```
67
-
bool
68
67
break
69
-
byte
70
68
continue
71
69
const
72
70
else
73
71
for
74
72
function
75
-
if
76
-
import
73
+
if
74
+
import
77
75
return
78
-
string
79
-
uint8
80
-
uint16
81
-
uint32
82
-
uint64
83
-
uint128
84
-
uint256
85
-
table
86
-
union
87
-
var
88
-
vector
76
+
range
77
+
var
89
78
```
90
79
91
80
## Operators and punctuation
@@ -245,6 +234,10 @@ function(a, _ uint32, z uint64) bool
245
234
246
235
# Complex Types
247
236
237
+
## Array types
238
+
239
+
TODO
240
+
248
241
## Vector types
249
242
An vector is a numbered sequence of elements of a single type, called the element type. The number of elements is called the length of the vector and is never negative.
250
243
@@ -256,6 +249,9 @@ ElementType = Type .
256
249
257
250
The length is part of the vector's type; it must evaluate to a non-negative constant representable by a value of type int. The length of vector a can be discovered using the built-in function size. The elements can be addressed by integer indices 0 through len(a)-1. Vector types are always one-dimensional but may be composed to form multi-dimensional types.
258
251
252
+
## Struct
253
+
254
+
TODO
259
255
260
256
## Table types
261
257
@@ -297,10 +293,30 @@ TODO
297
293
298
294
## Predeclared identifiers
299
295
296
+
The following identifiers are implicitly declared in the universe block
297
+
298
+
```
299
+
Types:
300
+
bool byte string
301
+
uint8 uint16 uint32 uint64 uint128 uint256
302
+
303
+
Constants:
304
+
true false
305
+
306
+
Zero value:
307
+
null
308
+
309
+
Functions:
310
+
append len max min
311
+
```
312
+
300
313
TODO
301
314
302
315
## Constant declarations
303
316
317
+
```
318
+
const a, b, c = 3, 4, "foo" // a = 3, b = 4, c = "foo", untyped integer and string constants
If the function's signature declares result parameters, the function body's statement list must end in a terminating statement.
334
369
335
-
370
+
```
371
+
func min(x uint8, y uint8) uint8{
372
+
if x < y {
373
+
return x
374
+
}
375
+
return y
376
+
}
377
+
```
336
378
337
379
# Expressions
338
380
@@ -363,41 +405,222 @@ A qualified identifier accesses an identifier in a different package, which must
363
405
math.Sin // denotes the Sin function in package math
364
406
```
365
407
408
+
## Index expressions
409
+
410
+
A primary expression of the form
411
+
412
+
```
413
+
a[x]
414
+
```
415
+
366
416
# Statements
367
417
368
-
if
369
-
for
370
-
break
371
-
continue
372
-
return
418
+
## Assignment statements
373
419
420
+
An assignment replaces the current value stored in a variable with a new value specified by an expression. An assignment statement may assign a single value to a single variable, or multiple values to a matching number of variables.
Each left-hand side operand must be addressable, a map index expression, or (for = assignments only) the blank identifier. Operands may be parenthesized.
429
+
430
+
```
431
+
x = 1
432
+
a[i] = 23
433
+
```
434
+
435
+
```
436
+
a, b = b, a // exchange a and b
437
+
438
+
x := []int{1, 2, 3}
439
+
i := 0
440
+
i, x[i] = 1, 2 // set i = 1, x[0] = 2
441
+
442
+
i = 0
443
+
x[i], i = 2, 1 // set x[0] = 2, i = 1
444
+
445
+
x[0], x[0] = 1, 2 // set x[0] = 1, then x[0] = 2 (so x[0] == 2 at end)
446
+
447
+
x[1], x[3] = 4, 5 // set x[1] = 4, then panic setting x[3] = 5.
448
+
449
+
type Point struct { x, y int }
450
+
var p *Point
451
+
x[2], p.x = 6, 7 // set x[2] = 6, then panic setting p.x = 7
452
+
453
+
i = 2
454
+
x = []int{3, 5, 7}
455
+
for i, x[i] = range x { // set i, x[2] = 0, x[0]
456
+
break
457
+
}
458
+
// after this loop, i == 0 and x is []int{3, 5, 3}
459
+
```
460
+
## If statements
461
+
462
+
"If" statements specify the conditional execution of two branches according to the value of a boolean expression. If the expression evaluates to true, the "if" branch is executed, otherwise, if present, the "else" branch is executed.
A "for" statement specifies repeated execution of a block. There are three forms: The iteration may be controlled by a single condition, a "for" clause, or a "range" clause.
478
+
479
+
```
480
+
for a < b {
481
+
a *= 2
482
+
}
483
+
```
484
+
485
+
```
486
+
for i := 0; i < 10; i++ {
487
+
f(i)
488
+
}
489
+
```
490
+
491
+
```
492
+
var a [10]string
493
+
for i, s := range a {
494
+
// type of i is int
495
+
// type of s is string
496
+
// s == a[i]
497
+
g(i, s)
498
+
}
499
+
500
+
```
501
+
502
+
## Break statements
503
+
504
+
A "break" statement terminates execution of the innermost "for" statement within the same function.
505
+
506
+
```
507
+
BreakStmt = "break" [ Label ] .
508
+
```
509
+
510
+
```
511
+
for i = 0; i < n; i++ {
512
+
for j = 0; j < m; j++ {
513
+
if a[i][j] == nil {
514
+
state = Error
515
+
break OuterLoop
516
+
} else if a[i][j] == item {
517
+
state = Found
518
+
break OuterLoop
519
+
}
520
+
}
521
+
}
522
+
```
523
+
524
+
## Continue statements
525
+
526
+
A "continue" statement begins the next iteration of the innermost enclosing "for" loop by advancing control to the end of the loop block. The "for" loop must be within the same function.
527
+
528
+
```
529
+
ContinueStmt = "continue" [ Label ] .
530
+
```
531
+
532
+
```
533
+
for y, row := range rows {
534
+
for x, data := range row {
535
+
if data == endOfRow {
536
+
continue RowLoop
537
+
}
538
+
row[x] = data + bias(x, y)
539
+
}
540
+
}
541
+
```
542
+
543
+
## Return statements
544
+
545
+
A "return" statement in a function F terminates the execution of F, and optionally provides one or more result values. Any functions deferred by F are executed before F returns to its caller.
546
+
547
+
```
548
+
ReturnStmt = "return" [ ExpressionList ] .
549
+
```
550
+
551
+
In a function without a result type, a "return" statement must not specify any result values.
552
+
553
+
```
554
+
func noResult() {
555
+
return
556
+
}
557
+
```
376
558
377
559
# Built-in functions
378
560
561
+
Built-in functions are predeclared. They are called like any other function but some of them accept a type instead of an expression as the first argument.
562
+
563
+
The built-in functions do not have standard Go types, so they can only appear in call expressions; they cannot be used as function values.
564
+
379
565
TODO
380
566
567
+
## Length
568
+
569
+
The built-in functions len take arguments of various types and return a result of type int. The implementation guarantees that the result always fits into an int.
570
+
571
+
```
572
+
Call Argument type Result
573
+
574
+
len(s) string type string length in bytes
575
+
[n]T, *[n]T array length (== n)
576
+
[]T slice length
577
+
578
+
```
579
+
580
+
## Min and max
581
+
582
+
The built-in functions min and max compute the smallest—or largest, respectively—value of a fixed number of arguments of ordered types. There must be at least one argument.
583
+
584
+
The same type rules as for operators apply: for ordered arguments x and y, min(x, y) is valid if x + y is valid, and the type of min(x, y) is the type of x + y (and similarly for max). If all arguments are constant, the result is constant.
585
+
586
+
```
587
+
var x, y int
588
+
m := min(x) // m == x
589
+
m := min(x, y) // m is the smaller of x and y
590
+
m := max(x, y, 10) // m is the larger of x and y but at least 10
591
+
var s []string
592
+
_ = min(s...) // invalid: slice arguments are not permitted
593
+
t := max("", "foo", "bar") // t == "foo" (string kind)
594
+
```
595
+
381
596
# Packages
382
597
598
+
TODO
383
599
384
600
# Program initialization and execution
385
601
602
+
TODO
386
603
387
604
# Errors
388
605
606
+
TODO
389
607
390
608
# Misc
391
609
610
+
TODO
392
611
393
612
## tx
394
613
614
+
TODO
395
615
396
616
## debug
397
617
398
618
Support limited print function. Formatting is not support.
0 commit comments