Skip to content

Commit

Permalink
Merge branch 'master' into clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcgrana committed Sep 1, 2019
2 parents dc33273 + 99505c3 commit ee5c86d
Show file tree
Hide file tree
Showing 251 changed files with 6,647 additions and 3,997 deletions.
2 changes: 0 additions & 2 deletions .env

This file was deleted.

3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public/* linguist-generated=true
vendor/* linguist-vendored=true

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.anvil
*.pyc
.idea
.vscode
1 change: 0 additions & 1 deletion .godir

This file was deleted.

18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: go

go:
- 1.12

before_install:
# We need Python to run pygmentize for generating HTML.
- sudo apt-get update
- sudo apt-get install python

install:
- go get -u github.com/russross/blackfriday

script:
- tools/build

env:
- VERBOSE=1 TESTING=1
22 changes: 11 additions & 11 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
## Contributing

Go by Example is now in a steady state. We are maintaining it, but are not
expanding or significantly changing it any more.
Thanks for your interest in contributing to Go by Example!

With that in mind here are some specific contribution guidelines:
* When sending a PR that affects the displayed contents of the site, run
`tools/build` locally and include the generated HTML in the PR. If you
only want to submit a simple typo suggestion (for example, through the
Github website), feel free to send a PR anyway - we'll regenerate the
HTML and merge with your commit.

* If you see a typo or would like to suggest another small change, editing the
.go or .sh source file should be sufficient for the PR. I can rebuild the
HTML when I review the change.
* We're open to adding more examples to the site. They should be on things
used by many programmers and only require the standard library. If you're
interested in adding an example, _please open an issue to discuss the topic
first_.

* We are not going to add any more sections to the site. There are many
important topics that Go by Example doesn't cover, which we leave to other
resources.

* We are not going to change the navigation of the site, in particular adding
* We're not going to change the navigation of the site, in particular adding
a "previous section" link or an "index" link other than the on the title
text.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
## Go by Example
# Go by Example

Content and build toolchain for [Go by Example](https://gobyexample.com),
a site that teaches Go via annotated example programs.


### Overview

The Go by Example site is built by extracting code and
Expand All @@ -17,9 +16,10 @@ The built `public` directory can be served by any
static content system. The production site uses S3 and
CloudFront, for example.


### Building

[![Build Status](https://travis-ci.com/mmcgrana/gobyexample.svg "Travis CI status")](https://travis-ci.com/mmcgrana/gobyexample)

To build the site you'll need Go and Python installed. Run:

```console
Expand All @@ -34,6 +34,16 @@ To build continuously in a loop:
$ tools/build-loop
```

### Publishing

To upload the site:

```console
$ gem install aws-sdk
$ export AWS_ACCESS_KEY_ID=...
$ export AWS_SECRET_ACCESS_KEY=...
$ tools/upload
```

### License

Expand All @@ -48,10 +58,13 @@ The Go Gopher is copyright [Renée French](http://reneefrench.blogspot.com/) and

Contributor translations of the Go by Example site are available in:

* [Chinese](http://gobyexample.everyx.in/) by [everyx](https://github.com/everyx)
* [Chinese](https://gobyexample.xgwang.me/) by [xg-wang](https://github.com/xg-wang/gobyexample)
* [French](http://le-go-par-l-exemple.keiruaprod.fr) by [keirua](https://github.com/keirua/gobyexample)
* [Italian](http://gobyexample.it) by the [Go Italian community](https://github.com/golangit/gobyexample-it)
* [Japanese](http://spinute.org/go-by-example) by [spinute](https://github.com/spinute)
* [Korean](https://mingrammer.com/gobyexample/) by [mingrammer](https://github.com/mingrammer)
* [Spanish](http://goconejemplos.com) by the [Go Mexico community](https://github.com/dabit/gobyexample)
* [Ukrainian](http://gobyexample.com.ua/) by [butuzov](https://github.com/butuzov/gobyexample)

### Thanks

Expand Down
7 changes: 7 additions & 0 deletions examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Range over Channels
Timers
Tickers
Worker Pools
WaitGroups
Rate Limiting
Atomic Counters
Mutexes
Expand All @@ -56,9 +57,15 @@ Base64 Encoding
Reading Files
Writing Files
Line Filters
File Paths
Directories
Temporary Files and Directories
Command-Line Arguments
Command-Line Flags
Command-Line Subcommands
Environment Variables
HTTP Clients
HTTP Servers
Spawning Processes
Exec'ing Processes
Signals
Expand Down
64 changes: 32 additions & 32 deletions examples/arrays/arrays.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,36 @@ import "fmt"

func main() {

// Here we create an array `a` that will hold exactly
// 5 `int`s. The type of elements and length are both
// part of the array's type. By default an array is
// zero-valued, which for `int`s means `0`s.
var a [5]int
fmt.Println("emp:", a)

// We can set a value at an index using the
// `array[index] = value` syntax, and get a value with
// `array[index]`.
a[4] = 100
fmt.Println("set:", a)
fmt.Println("get:", a[4])

// The builtin `len` returns the length of an array.
fmt.Println("len:", len(a))

// Use this syntax to declare and initialize an array
// in one line.
b := [5]int{1, 2, 3, 4, 5}
fmt.Println("dcl:", b)

// Array types are one-dimensional, but you can
// compose types to build multi-dimensional data
// structures.
var twoD [2][3]int
for i := 0; i < 2; i++ {
for j := 0; j < 3; j++ {
twoD[i][j] = i + j
}
}
fmt.Println("2d: ", twoD)
// Here we create an array `a` that will hold exactly
// 5 `int`s. The type of elements and length are both
// part of the array's type. By default an array is
// zero-valued, which for `int`s means `0`s.
var a [5]int
fmt.Println("emp:", a)

// We can set a value at an index using the
// `array[index] = value` syntax, and get a value with
// `array[index]`.
a[4] = 100
fmt.Println("set:", a)
fmt.Println("get:", a[4])

// The builtin `len` returns the length of an array.
fmt.Println("len:", len(a))

// Use this syntax to declare and initialize an array
// in one line.
b := [5]int{1, 2, 3, 4, 5}
fmt.Println("dcl:", b)

// Array types are one-dimensional, but you can
// compose types to build multi-dimensional data
// structures.
var twoD [2][3]int
for i := 0; i < 2; i++ {
for j := 0; j < 3; j++ {
twoD[i][j] = i + j
}
}
fmt.Println("2d: ", twoD)
}
2 changes: 1 addition & 1 deletion examples/arrays/arrays.hash
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
305975d13d24223181d13f042b290906d86c1a0e
l-A8eBnwio
W7NwfDq8Vdw
66 changes: 33 additions & 33 deletions examples/atomic-counters/atomic-counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,37 @@ import "sync/atomic"

func main() {

// We'll use an unsigned integer to represent our
// (always-positive) counter.
var ops uint64 = 0

// To simulate concurrent updates, we'll start 50
// goroutines that each increment the counter about
// once a millisecond.
for i := 0; i < 50; i++ {
go func() {
for {
// To atomically increment the counter we
// use `AddUint64`, giving it the memory
// address of our `ops` counter with the
// `&` syntax.
atomic.AddUint64(&ops, 1)

// Wait a bit between increments.
time.Sleep(time.Millisecond)
}
}()
}

// Wait a second to allow some ops to accumulate.
time.Sleep(time.Second)

// In order to safely use the counter while it's still
// being updated by other goroutines, we extract a
// copy of the current value into `opsFinal` via
// `LoadUint64`. As above we need to give this
// function the memory address `&ops` from which to
// fetch the value.
opsFinal := atomic.LoadUint64(&ops)
fmt.Println("ops:", opsFinal)
// We'll use an unsigned integer to represent our
// (always-positive) counter.
var ops uint64

// To simulate concurrent updates, we'll start 50
// goroutines that each increment the counter about
// once a millisecond.
for i := 0; i < 50; i++ {
go func() {
for {
// To atomically increment the counter we
// use `AddUint64`, giving it the memory
// address of our `ops` counter with the
// `&` syntax.
atomic.AddUint64(&ops, 1)

// Wait a bit between increments.
time.Sleep(time.Millisecond)
}
}()
}

// Wait a second to allow some ops to accumulate.
time.Sleep(time.Second)

// In order to safely use the counter while it's still
// being updated by other goroutines, we extract a
// copy of the current value into `opsFinal` via
// `LoadUint64`. As above we need to give this
// function the memory address `&ops` from which to
// fetch the value.
opsFinal := atomic.LoadUint64(&ops)
fmt.Println("ops:", opsFinal)
}
4 changes: 2 additions & 2 deletions examples/atomic-counters/atomic-counters.hash
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ce8821f1f4fd99d554ad6cde52403dd3b69bb70a
8p48eFFxDZ
a4190094ea0405b5f2733101beb97939a1d43aee
KDr9EMMPMgi
40 changes: 20 additions & 20 deletions examples/base64-encoding/base64-encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ import "fmt"

func main() {

// Here's the `string` we'll encode/decode.
data := "abc123!?$*&()'-=@~"
// Here's the `string` we'll encode/decode.
data := "abc123!?$*&()'-=@~"

// Go supports both standard and URL-compatible
// base64. Here's how to encode using the standard
// encoder. The encoder requires a `[]byte` so we
// cast our `string` to that type.
sEnc := b64.StdEncoding.EncodeToString([]byte(data))
fmt.Println(sEnc)
// Go supports both standard and URL-compatible
// base64. Here's how to encode using the standard
// encoder. The encoder requires a `[]byte` so we
// convert our `string` to that type.
sEnc := b64.StdEncoding.EncodeToString([]byte(data))
fmt.Println(sEnc)

// Decoding may return an error, which you can check
// if you don't already know the input to be
// well-formed.
sDec, _ := b64.StdEncoding.DecodeString(sEnc)
fmt.Println(string(sDec))
fmt.Println()
// Decoding may return an error, which you can check
// if you don't already know the input to be
// well-formed.
sDec, _ := b64.StdEncoding.DecodeString(sEnc)
fmt.Println(string(sDec))
fmt.Println()

// This encodes/decodes using a URL-compatible base64
// format.
uEnc := b64.URLEncoding.EncodeToString([]byte(data))
fmt.Println(uEnc)
uDec, _ := b64.URLEncoding.DecodeString(uEnc)
fmt.Println(string(uDec))
// This encodes/decodes using a URL-compatible base64
// format.
uEnc := b64.URLEncoding.EncodeToString([]byte(data))
fmt.Println(uEnc)
uDec, _ := b64.URLEncoding.DecodeString(uEnc)
fmt.Println(string(uDec))
}
4 changes: 2 additions & 2 deletions examples/base64-encoding/base64-encoding.hash
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
e57f5be3a796261fb4a55cdb0580a254e14b4930
t6rFm2x4Yr
c20da14820b656c867790f2e99bc37140babca8c
y_QTcqdkvZh
22 changes: 11 additions & 11 deletions examples/channel-buffering/channel-buffering.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import "fmt"

func main() {

// Here we `make` a channel of strings buffering up to
// 2 values.
messages := make(chan string, 2)
// Here we `make` a channel of strings buffering up to
// 2 values.
messages := make(chan string, 2)

// Because this channel is buffered, we can send these
// values into the channel without a corresponding
// concurrent receive.
messages <- "buffered"
messages <- "channel"
// Because this channel is buffered, we can send these
// values into the channel without a corresponding
// concurrent receive.
messages <- "buffered"
messages <- "channel"

// Later we can receive these two values as usual.
fmt.Println(<-messages)
fmt.Println(<-messages)
// Later we can receive these two values as usual.
fmt.Println(<-messages)
fmt.Println(<-messages)
}
2 changes: 1 addition & 1 deletion examples/channel-buffering/channel-buffering.hash
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
122140f7ad1bc5cff4fcd7a9e7245b87aaca3ec5
34PVHwO6Bn
mPoF-Xi-rip
Loading

0 comments on commit ee5c86d

Please sign in to comment.