Skip to content

Commit 67d449c

Browse files
authored
save
1 parent 0952116 commit 67d449c

File tree

13 files changed

+265
-217
lines changed

13 files changed

+265
-217
lines changed

content/en/_index.md

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
1-
---
2-
weight: 1
3-
---
1+
# Go by Example
42

5-
Hello world
3+
[Go](https://go.dev/) is an open-source programming language designed for building scalable, secure, and reliable software. Please read the [official documentation](https://go.dev/doc/tutorial/getting-started) to learn more.
4+
5+
*Go by Example* is a hands-on introduction to Go using annotated example programs. Check out the [first example](hello-world.md) or browse the full list below.
6+
7+
Unless stated otherwise, the examples here assume the [latest major release Go](https://go.dev/doc/devel/release) and may use new language features. If something isn't working, try upgrading to the latest version.
8+
9+
{{< docstoc >}}
10+
11+
### FAQ
12+
13+
#### I found a problem with the examples; what do I do?
14+
15+
We're very happy to fix problem reports and accept contributions! Please submit
16+
[an issue](https://github.com/jcbhmt/gobyexample.jcbhmr.com/issues) or send a Pull Request.
17+
See `CONTRIBUTING.md` for more details.
18+
19+
#### What version of Go is required to run these examples?
20+
21+
Given Go's strong [backwards compatibility guarantees](https://go.dev/doc/go1compat),
22+
we expect the vast majority of examples to work on the latest released version of Go
23+
as well as many older releases going back years.
24+
25+
That said, some examples show off new features added in recent releases; therefore,
26+
it's recommended to try running examples with the latest officially released Go version
27+
(see Go's [release history](https://go.dev/doc/devel/release) for details).
28+
29+
#### I'm getting output in a different order from the example. Is the example wrong?
30+
31+
Some of the examples demonstrate concurrent code which has a non-deterministic
32+
execution order. It depends on how the Go runtime schedules its goroutines and
33+
may vary by operating system, CPU architecture, or even Go version.
34+
35+
Similarly, examples that iterate over maps may produce items in a different order
36+
from what you're getting on your machine. This is because the order of iteration
37+
over maps in Go is [not specified and is not guaranteed to be the same from one
38+
iteration to the next](https://go.dev/ref/spec#RangeClause).
39+
40+
It doesn't mean anything is wrong with the example. Typically the code in these
41+
examples will be insensitive to the actual order of the output; if the code is
42+
sensitive to the order - that's probably a bug - so feel free to report it.

content/en/docs/README.md

Lines changed: 0 additions & 52 deletions
This file was deleted.

content/en/docs/SUMMARY.md renamed to content/en/docs/SUMMARY.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
# Summary
2-
3-
[Go by Example](README.md)
4-
5-
---
6-
71
- [Hello World](hello-world.md)
82
- [Values](values.md)
93
- [Variables](variables.md)

content/en/docs/constants.md

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,66 @@
1+
---
2+
weight: 4
3+
---
4+
15
# Constants
26

3-
```go,editable
4-
// Go supports _constants_ of character, string, boolean,
5-
// and numeric values.
7+
Go supports _constants_ of character, string, boolean,
8+
and numeric values.
9+
10+
`const` declares a constant value.
611

12+
```go
713
package main
814

9-
import (
10-
"fmt"
11-
"math"
12-
)
15+
import "fmt"
1316

14-
// `const` declares a constant value.
1517
const s string = "constant"
1618

1719
func main() {
1820
fmt.Println(s)
21+
// Output: constant
22+
}
23+
```
24+
25+
A `const` statement can appear anywhere a `var`
26+
statement can.
27+
28+
```go
29+
package main
1930

20-
// A `const` statement can appear anywhere a `var`
21-
// statement can.
31+
func main() {
2232
const n = 500000000
33+
fmt.Println(n)
34+
// Output: 500000000
35+
}
36+
```
2337

24-
// Constant expressions perform arithmetic with
25-
// arbitrary precision.
26-
const d = 3e20 / n
27-
fmt.Println(d)
38+
Constant expressions perform arithmetic with
39+
arbitrary precision.
2840

29-
// A numeric constant has no type until it's given
30-
// one, such as by an explicit conversion.
31-
fmt.Println(int64(d))
41+
```go
42+
const n = 500000000
43+
const d = 3e20 / n
44+
fmt.Println(d)
45+
// Output: 6e+11
46+
```
3247

33-
// A number can be given a type by using it in a
34-
// context that requires one, such as a variable
35-
// assignment or function call. For example, here
36-
// `math.Sin` expects a `float64`.
37-
fmt.Println(math.Sin(n))
38-
}
48+
A numeric constant has no type until it's given
49+
one, such as by an explicit conversion.
50+
51+
```go
52+
const d = 6e+11
53+
fmt.Println(int64(d))
54+
// Output: 600000000000
3955
```
4056

41-
```sh
42-
$ go run constant.go
43-
constant
44-
6e+11
45-
600000000000
46-
-0.28470407323754404
57+
A number can be given a type by using it in a
58+
context that requires one, such as a variable
59+
assignment or function call. For example, here
60+
`math.Sin` expects a `float64`.
61+
62+
```go
63+
const n = 500000000
64+
fmt.Println(math.Sin(n))
65+
// Output: -0.28470407323754404
4766
```

content/en/docs/for.md

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,79 @@
1+
---
2+
weight: 5
3+
---
4+
15
# For
26

3-
```go,editable
4-
// `for` is Go's only looping construct. Here are
5-
// some basic types of `for` loops.
7+
`for` is Go's only looping construct. Here are
8+
some basic types of `for` loops.
69

7-
package main
10+
The most basic type, with a single condition.
811

9-
import "fmt"
12+
```go
13+
i := 1
14+
for i <= 3 {
15+
fmt.Println(i)
16+
i = i + 1
17+
}
18+
// Output:
19+
// 1
20+
// 2
21+
// 3
22+
```
1023

11-
func main() {
24+
A classic initial/condition/after `for` loop.
1225

13-
// The most basic type, with a single condition.
14-
i := 1
15-
for i <= 3 {
16-
fmt.Println(i)
17-
i = i + 1
18-
}
26+
```go
27+
for j := 0; j < 3; j++ {
28+
fmt.Println(j)
29+
}
30+
// Output:
31+
// 0
32+
// 1
33+
// 2
34+
```
1935

20-
// A classic initial/condition/after `for` loop.
21-
for j := 0; j < 3; j++ {
22-
fmt.Println(j)
23-
}
36+
Another way of accomplishing the basic "do this
37+
N times" iteration is `range` over an integer.
2438

25-
// Another way of accomplishing the basic "do this
26-
// N times" iteration is `range` over an integer.
27-
for i := range 3 {
28-
fmt.Println("range", i)
29-
}
39+
```go
40+
for i := range 3 {
41+
fmt.Println("range", i)
42+
}
43+
// Output:
44+
// range 0
45+
// range 1
46+
// range 2
47+
```
3048

31-
// `for` without a condition will loop repeatedly
32-
// until you `break` out of the loop or `return` from
33-
// the enclosing function.
34-
for {
35-
fmt.Println("loop")
36-
break
37-
}
49+
`for` without a condition will loop repeatedly
50+
until you `break` out of the loop or `return` from
51+
the enclosing function.
3852

39-
// You can also `continue` to the next iteration of
40-
// the loop.
41-
for n := range 6 {
42-
if n%2 == 0 {
43-
continue
44-
}
45-
fmt.Println(n)
46-
}
53+
```go
54+
for {
55+
fmt.Println("loop")
56+
break
4757
}
58+
// Output: loop
4859
```
4960

50-
```sh
51-
$ go run for.go
52-
1
53-
2
54-
3
55-
0
56-
1
57-
2
58-
range 0
59-
range 1
60-
range 2
61-
loop
62-
1
63-
3
64-
5
61+
You can also `continue` to the next iteration of
62+
the loop.
6563

66-
# We'll see some other `for` forms later when we look at
67-
# `range` statements, channels, and other data
68-
# structures.
64+
```go
65+
for n := range 6 {
66+
if n%2 == 0 {
67+
continue
68+
}
69+
fmt.Println(n)
70+
}
71+
// Output:
72+
// 1
73+
// 3
74+
// 5
6975
```
76+
77+
We'll see some other `for` forms later when we look at
78+
`range` statements, channels, and other data
79+
structures.

content/en/docs/hello-world.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
---
2+
weight: 1
3+
---
4+
15
# Hello World
26

3-
```go,editable
4-
// Our first program will print the classic "hello world"
5-
// message. Here's the full source code.
7+
Our first program will print the classic "hello world"
8+
message. Here's the full source code.
9+
10+
{{% goplay %}}
11+
12+
```go
613
package main
714

815
import "fmt"
@@ -12,22 +19,31 @@ func main() {
1219
}
1320
```
1421

22+
{{% /goplay %}}
23+
24+
To run the program, put the code in `hello-world.go` and
25+
use `go run`.
26+
1527
```sh
16-
# To run the program, put the code in `hello-world.go` and
17-
# use `go run`.
1828
$ go run hello-world.go
1929
hello world
30+
```
31+
32+
Sometimes we'll want to build our programs into
33+
binaries. We can do this using `go build`.
2034

21-
# Sometimes we'll want to build our programs into
22-
# binaries. We can do this using `go build`.
35+
```sh
2336
$ go build hello-world.go
2437
$ ls
2538
hello-world hello-world.go
39+
```
40+
41+
We can then execute the built binary directly.
2642

27-
# We can then execute the built binary directly.
43+
```sh
2844
$ ./hello-world
2945
hello world
30-
31-
# Now that we can run and build basic Go programs, let's
32-
# learn more about the language.
3346
```
47+
48+
Now that we can run and build basic Go programs, let's
49+
learn more about the language.

0 commit comments

Comments
 (0)