Skip to content

Commit

Permalink
Merge pull request #16 from campoy/embedmd
Browse files Browse the repository at this point in the history
Use embedmd for all code snippets
  • Loading branch information
campoy committed Feb 25, 2017
2 parents 45ef74b + 6369f10 commit a9bd677
Show file tree
Hide file tree
Showing 64 changed files with 593 additions and 255 deletions.
2 changes: 1 addition & 1 deletion events/step0/events.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step0/static/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
Copyright 2016 Google Inc. All rights reserved.
Copyright 2017 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step0/static/script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Google Inc. All rights reserved.
* Copyright 2017 Google Inc. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step0/static/style.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Google Inc. All rights reserved.
* Copyright 2017 Google Inc. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step1/events.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step1/static/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
Copyright 2016 Google Inc. All rights reserved.
Copyright 2017 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step1/static/script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Google Inc. All rights reserved.
* Copyright 2017 Google Inc. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step1/static/style.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Google Inc. All rights reserved.
* Copyright 2017 Google Inc. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step2/events.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step2/static/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
Copyright 2016 Google Inc. All rights reserved.
Copyright 2017 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step2/static/script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Google Inc. All rights reserved.
* Copyright 2017 Google Inc. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step2/static/style.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Google Inc. All rights reserved.
* Copyright 2017 Google Inc. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step3/events.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step3/static/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
Copyright 2016 Google Inc. All rights reserved.
Copyright 2017 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step3/static/script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Google Inc. All rights reserved.
* Copyright 2017 Google Inc. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step3/static/style.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Google Inc. All rights reserved.
* Copyright 2017 Google Inc. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step3/weather.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step4/events.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step4/static/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
Copyright 2016 Google Inc. All rights reserved.
Copyright 2017 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step4/static/script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Google Inc. All rights reserved.
* Copyright 2017 Google Inc. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step4/static/style.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Google Inc. All rights reserved.
* Copyright 2017 Google Inc. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step4/weather.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step5/events.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step5/static/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
Copyright 2016 Google Inc. All rights reserved.
Copyright 2017 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step5/static/script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Google Inc. All rights reserved.
* Copyright 2017 Google Inc. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step5/static/style.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Google Inc. All rights reserved.
* Copyright 2017 Google Inc. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion events/step5/weather.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down
20 changes: 10 additions & 10 deletions section00/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

Before we start writing web servers let's analyze a simple Go program.

[embedmd]:# (hello/main.go /package main/ $)
```go
package main

import "fmt"
import "fmt"

func main() {
fmt.Println( "hello, world!" );
}
func main() {
fmt.Println("hello, world!") }
```

You should be able to understand every line of this program.
Expand Down Expand Up @@ -42,18 +42,18 @@ But this doesn't mean you need to write them yourself!

Try deleting the line `import "fmt"` from `main.go` and run it, you should see an error:

```
$ go run main.go
# command-line-arguments
./main.go:5: undefined: fmt in fmt.Println
```bash
$ go run main.go
# command-line-arguments
./main.go:5: undefined: fmt in fmt.Println
```

You can fix this error by manually adding the missing import statements or using `goimports`.

If you don't have `goimports` installed in your machine you can easily install it by running:

```
$ go get golang.org/x/tools/cmd/goimports
```bash
$ go get golang.org/x/tools/cmd/goimports
```

This will install the `goimports` binary in `$GOAPTH/bin`.
Expand Down
9 changes: 4 additions & 5 deletions section00/hello/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand All @@ -13,8 +13,7 @@

package main

import "fmt"
import "fmt"

func main() {
fmt.Println("hello, world!")
}
func main() {
fmt.Println("hello, world!") }
21 changes: 12 additions & 9 deletions section01/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,23 @@ But before that it is important to realize that there's helper functions that wi

One of those helper functions is [`Get`](https://golang.org/pkg/net/http#Get).

[embedmd]:# (examples/get.go /package main/ $)
```go
package main

import (
"fmt"
"log"
"net/http"
"fmt"
"log"
"net/http"
)

func main() {
res, err := http.Get("https://golang.org")
if err != nil {
log.Fatal(err)
}
fmt.Println(res.Status)
// try changing the value of this url
res, err := http.Get("https://golang.org")
if err != nil {
log.Fatal(err)
}
fmt.Println(res.Status)
}
```
[source code](examples/get.go)
Expand Down Expand Up @@ -84,6 +86,7 @@ For instance, we can create the equivalent request to the original `get.go` prog

The `Client` type exposes the `Do` method that send the given `Request` and returns a `Response` and an `error`.

[embedmd]:# (examples/do-get.go /package main/ $)
```go
package main

Expand Down Expand Up @@ -121,7 +124,7 @@ Replace `YourName` with your name, or something unique that no one else might be
This will save whatever string you send in the Body of the request so you can retrieve it later with:

```
$ curl https://http-methods.appspot.com/YourName/Message
$ curl https://http-methods.appspot.com/YourName/Message
```

## Parameters: queries and forms
Expand Down
2 changes: 1 addition & 1 deletion section01/examples/do-get.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion section01/examples/get.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down
19 changes: 13 additions & 6 deletions section02/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ method which makes it satisfy the `io.Writer` interface.

Let's see a very simple HTTP handler that simply writes `"Hello, web"` to the output:

[embedmd]:# (examples/step1.go /package main/ $)
```go
package main

Expand Down Expand Up @@ -57,6 +58,7 @@ for any other paths in the `"/images/"` subtree.

Let's see how to register our `helloHandler` defined above:

[embedmd]:# (examples/step2.go /package main/ $)
```go
package main

Expand All @@ -73,11 +75,14 @@ func main() {
http.HandleFunc("/hello", helloHandler)
}
```

Note that we're registering our handler as part of the `main` function.

Try to run the code above:

$ go run main.go
```bash
$ go run examples/step2.go
```

What happens? Well, we're missing the last piece of the puzzle: starting the
web server!
Expand Down Expand Up @@ -145,6 +150,7 @@ when no error has occurred the returned value equals to `nil`.
So if we want to check that our server started successfully and log an error
otherwise we would modify our code to add a call to `ListenAndServe`.

[embedmd]:# (examples/step3.go /package main/ $)
```go
package main

Expand All @@ -167,8 +173,6 @@ func main() {
}
```

_Note_: you can find the complete code on [main.go](main.go).

Running this code should now start a web server listening on `127.0.0.1:8080`.

Try it:
Expand Down Expand Up @@ -200,12 +204,13 @@ These cases can be handled either by hand or using a toolkit that will plug
correctly into the existing `net/http` package, such as the
[Gorilla toolkit](http://www.gorillatoolkit.org/) and its `mux` package.


[embedmd]:# (examples/gorilla.go /package main/ $)
```go
package main

import (
...
"log"
"net/http"

"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -236,7 +241,9 @@ func main() {

// handle all requests with the Gorilla router.
http.Handle("/", r)
http.ListenAndServe("127.0.0.1:8080", nil)
if err := http.ListenAndServe("127.0.0.1:8080", nil); err != nil {
log.Fatal(err)
}
}
```

Expand Down

0 comments on commit a9bd677

Please sign in to comment.