Skip to content

Commit eb5220f

Browse files
committed
add english version
1 parent 946300f commit eb5220f

File tree

269 files changed

+16101
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

269 files changed

+16101
-0
lines changed

en/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
***Build Web Application with Golang***
2+
======================================
3+
4+
[Download PDF](https://drive.google.com/file/d/0B2GBHFyTK2N8TzM4dEtIWjBJdEk/edit?usp=sharing)
5+
6+
### ***Translator Comments***
7+
8+
This is an English version of [《Go Web编程》](https://github.com/astaxie/build-web-application-with-golang), the original version was written by [AstaXie](https://github.com/astaxie) and translated by [Unknown](https://github.com/Unknwon) and [Larry Battle](https://github.com/LarryBattle).
9+
10+
This book is about how to build web applications in Go. In the first few chapters of the book, the author will review some basic knowledge about Go. However, for an optimal reading experience, you should have a basic understanding of the Go language and the concept of a web application. If you are completely new to programming, this book is not intended to provide sufficient introductory material to get started.
11+
12+
If anything is unclear due to wording or language issues, feel free to ask me to write a better translation.
13+
14+
###Acknowledgments for translation help
15+
16+
- [matalangilbert](https://github.com/matalangilbert)
17+
- [nightlyone](https://github.com/nightlyone)
18+
- [sbinet](https://github.com/sbinet)
19+
- [carbocation](https://github.com/carbocation)
20+
- [desimone](https://github.com/desimone)
21+
- [reigai](https://github.com/reigai)
22+
- [OlingCat](https://github.com/OlingCat)
23+
24+
### Purpose
25+
26+
Because I'm interested in web application development, I used my free time to write this book as an open source version. It doesn't mean that I have a very good ability to build web applications; I would like to share what I've done with Go in building web applications.
27+
28+
- For those of you who are working with PHP/Python/Ruby, you will learn how to build a web application with Go.
29+
- For those of you who are working with C/C++, you will know how the web works.
30+
31+
I believe the purpose of studying is sharing with others. The happiest thing in my life is sharing everything I've known with more people.
32+
33+
### Donation
34+
35+
If you like this book, you can ask your Chinese friends to follow this [link](https://me.alipay.com/astaxie) donate the original author, help him write more books with better, more useful, and more interesting content.
36+
37+
### Exchanging Learning Go
38+
39+
If you know what is QQ, join the group 259316004. If not, follow this [link](http://download.imqq.com/download.shtml) to get more details. Also, you can join our [forum](http://bbs.beego.me).
40+
41+
### Acknowledgments
42+
43+
First, I have to thank the people who are members of Golang-China in QQ group 102319854, they are all very nice and helpful. Then, I need to thank the following people who gave great help when I was writing this book.
44+
45+
- [四月份平民 April Citizen](https://plus.google.com/110445767383269817959) (review code)
46+
- [洪瑞琦 Hong Ruiqi](https://github.com/hongruiqi) (review code)
47+
- [边 疆 BianJiang](https://github.com/border) (write the configurations about Vim and Emacs for Go development)
48+
- [欧林猫 Oling Cat](https://github.com/OlingCat)(review code)
49+
- [吴文磊 Wenlei Wu](mailto:[email protected])(provide some pictures)
50+
- [北极星 Polaris](https://github.com/polaris1119)(review whole book)
51+
- [雨 痕 Rain Trail](https://github.com/qyuhen)(review chapter 2 and 3)
52+
53+
### License
54+
55+
This book is licensed under the [CC BY-SA 3.0 License](http://creativecommons.org/licenses/by-sa/3.0/),
56+
the code is licensed under a [BSD 3-Clause License](<https://github.com/astaxie/build-web-application-with-golang/blob/master/LICENSE.md>), unless otherwise specified.
57+
58+
### Get Started
59+
60+
[Index](./eBook/preface.md)

en/code/readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Workspace setup.
2+
3+
To avoid workspace issues and be able to develop from any folder within this path,
4+
set the environment variable `GOPATH` to the path of this directory.
5+
6+
More info:
7+
- [GOPATH documentation](http://golang.org/doc/code.html#GOPATH)

en/code/src/apps/ch.1.2/main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Example code for Chapter 1.2 from "Build Web Application with Golang"
2+
// Purpose: Run this file to check if your workspace is setup correctly.
3+
// To run, navigate to the current directory in a console and type `go run main.go`
4+
// If the text "Hello World" isn't shown, then setup your workspace again.
5+
package main
6+
7+
import (
8+
"fmt"
9+
"mymath"
10+
)
11+
12+
func main() {
13+
fmt.Printf("Hello, world. Sqrt(2) = %v\n", mymath.Sqrt(2))
14+
}

en/code/src/apps/ch.2.1/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Example code for Chapter ? from "Build Web Application with Golang"
2+
// Purpose: Hello world example demonstrating UTF-8 support.
3+
// To run in the console, type `go run main.go`
4+
// You're missing language fonts, if you're seeing squares or question marks.
5+
package main
6+
7+
import "fmt"
8+
9+
func main() {
10+
fmt.Printf("Hello, world or 你好,世界 or καλημ ́ρα κóσμ or こんにちは世界\n")
11+
}

en/code/src/apps/ch.2.2/main.go

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
// Example code for Chapter 2.2 from "Build Web Application with Golang"
2+
// Purpose: Goes over the assignment and manipulation of basic data types.
3+
package main
4+
5+
import (
6+
"errors"
7+
"fmt"
8+
)
9+
10+
// constants
11+
const Pi = 3.1415926
12+
13+
// booleans default to `false`
14+
var isActive bool // global variable
15+
var enabled, disabled = true, false // omit type of variables
16+
17+
// grouped definitions
18+
const (
19+
i = 1e4
20+
MaxThread = 10
21+
prefix = "astaxie_"
22+
)
23+
24+
var (
25+
frenchHello string // basic form to define string
26+
emptyString string = "" // define a string with empty string
27+
)
28+
29+
func show_multiple_assignments() {
30+
fmt.Println("show_multiple_assignments()")
31+
var v1 int = 42
32+
33+
// Define three variables with type "int", and initialize their values.
34+
// vname1 is v1, vname2 is v2, vname3 is v3
35+
var v2, v3 int = 2, 3
36+
37+
// `:=` only works in functions
38+
// `:=` is the short way of declaring variables without
39+
// specifying the type and using the keyboard `var`.
40+
vname1, vname2, vname3 := v1, v2, v3
41+
42+
// `_` disregards the returned value.
43+
_, b := 34, 35
44+
45+
fmt.Printf("vname1 = %v, vname2 = %v, vname3 = %v\n", vname1, vname2, vname3)
46+
fmt.Printf("v1 = %v, v2 = %v, v3 = %v\n", v1, v2, v3)
47+
fmt.Println("b =", b)
48+
}
49+
func show_bool() {
50+
fmt.Println("show_bool()")
51+
var available bool // local variable
52+
valid := false // Shorthand assignment
53+
available = true // assign value to variable
54+
55+
fmt.Printf("valid = %v, !valid = %v\n", valid, !valid)
56+
fmt.Printf("available = %v\n", available)
57+
}
58+
func show_different_types() {
59+
fmt.Println("show_different_types()")
60+
var (
61+
unicodeChar rune
62+
a int8
63+
b int16
64+
c int32
65+
d int64
66+
e byte
67+
f uint8
68+
g int16
69+
h uint32
70+
i uint64
71+
)
72+
var cmplx complex64 = 5 + 5i
73+
74+
fmt.Println("Default values for int types")
75+
fmt.Println(unicodeChar, a, b, c, d, e, f, g, h, i)
76+
77+
fmt.Printf("Value is: %v\n", cmplx)
78+
}
79+
func show_strings() {
80+
fmt.Println("show_strings()")
81+
no, yes, maybe := "no", "yes", "maybe" // brief statement
82+
japaneseHello := "Ohaiyou"
83+
frenchHello = "Bonjour" // basic form of assign values
84+
85+
fmt.Println("Random strings")
86+
fmt.Println(frenchHello, japaneseHello, no, yes, maybe)
87+
88+
// The backtick, `, will not escape any character in a string
89+
fmt.Println(`This
90+
is on
91+
multiple lines`)
92+
}
93+
func show_string_manipulation() {
94+
fmt.Println("show_string_manipulation()")
95+
var s string = "hello"
96+
97+
//You can't do this with strings
98+
//s[0] = 'c'
99+
100+
s = "hello"
101+
c := []byte(s) // convert string to []byte type
102+
c[0] = 'c'
103+
s2 := string(c) // convert back to string type
104+
105+
m := " world"
106+
a := s + m
107+
108+
d := "c" + s[1:] // you cannot change string values by index, but you can get values instead.
109+
fmt.Printf("%s\n", d)
110+
111+
fmt.Printf("s = %s, c = %v\n", s, c)
112+
fmt.Printf("s2 = %s\n", s2)
113+
fmt.Printf("combined strings\na = %s, d = %s\n", a, d)
114+
}
115+
func show_errors() {
116+
fmt.Println("show_errors()")
117+
err := errors.New("Example error message\n")
118+
if err != nil {
119+
fmt.Print(err)
120+
}
121+
}
122+
func show_iota() {
123+
fmt.Println("show_iota()")
124+
const (
125+
x = iota // x == 0
126+
y = iota // y == 1
127+
z = iota // z == 2
128+
w // If there is no expression after constants name,
129+
// it uses the last expression, so here is saying w = iota implicitly.
130+
// Therefore w == 3, and y and x both can omit "= iota" as well.
131+
)
132+
133+
const v = iota // once iota meets keyword `const`, it resets to `0`, so v = 0.
134+
135+
const (
136+
e, f, g = iota, iota, iota // e=0,f=0,g=0 values of iota are same in one line.
137+
)
138+
fmt.Printf("x = %v, y = %v, z = %v, w = %v\n", x, y, z, w)
139+
fmt.Printf("v = %v\n", v)
140+
fmt.Printf("e = %v, f = %v, g = %v\n", e, f, g)
141+
}
142+
143+
// Functions and variables starting with a capital letter are public to other packages.
144+
// Everything else is private.
145+
func This_is_public() {}
146+
func this_is_private() {}
147+
148+
func set_default_values() {
149+
// default values for the types.
150+
const (
151+
a int = 0
152+
b int8 = 0
153+
c int32 = 0
154+
d int64 = 0
155+
e uint = 0x0
156+
f rune = 0 // the actual type of rune is int32
157+
g byte = 0x0 // the actual type of byte is uint8
158+
h float32 = 0 // length is 4 byte
159+
i float64 = 0 //length is 8 byte
160+
j bool = false
161+
k string = ""
162+
)
163+
}
164+
func show_arrays() {
165+
fmt.Println("show_arrays()")
166+
var arr [10]int // an array of type int
167+
arr[0] = 42 // array is 0-based
168+
arr[1] = 13 // assign value to element
169+
170+
a := [3]int{1, 2, 3} // define a int array with 3 elements
171+
172+
b := [10]int{1, 2, 3}
173+
// define a int array with 10 elements,
174+
// and first three are assigned, rest of them use default value 0.
175+
176+
c := [...]int{4, 5, 6} // use `…` replace with number of length, Go will calculate it for you.
177+
178+
// define a two-dimensional array with 2 elements, and each element has 4 elements.
179+
doubleArray := [2][4]int{[4]int{1, 2, 3, 4}, [4]int{5, 6, 7, 8}}
180+
181+
// You can write about declaration in a shorter way.
182+
easyArray := [2][4]int{{1, 2, 3, 4}, {5, 6, 7, 8}}
183+
184+
fmt.Println("arr =", arr)
185+
fmt.Printf("The first element is %d\n", arr[0]) // get element value, it returns 42
186+
fmt.Printf("The last element is %d\n", arr[9])
187+
//it returns default value of 10th element in this array, which is 0 in this case.
188+
189+
fmt.Println("array a =", a)
190+
fmt.Println("array b =", b)
191+
fmt.Println("array c =", c)
192+
193+
fmt.Println("array doubleArray =", doubleArray)
194+
fmt.Println("array easyArray =", easyArray)
195+
}
196+
func show_slices() {
197+
fmt.Println("show_slices()")
198+
// define a slice with 10 elements which types are byte
199+
var ar = [10]byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}
200+
201+
// define two slices with type []byte
202+
var a, b []byte
203+
204+
// a points to elements from 3rd to 5th in array ar.
205+
a = ar[2:5]
206+
// now a has elements ar[2]、ar[3] and ar[4]
207+
208+
// b is another slice of array ar
209+
b = ar[3:5]
210+
// now b has elements ar[3] and ar[4]
211+
212+
// define an array
213+
var array = [10]byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}
214+
// define two slices
215+
var aSlice, bSlice []byte
216+
217+
// some convenient operations
218+
aSlice = array[:3] // equals to aSlice = array[0:3] aSlice has elements a,b,c
219+
aSlice = array[5:] // equals to aSlice = array[5:10] aSlice has elements f,g,h,i,j
220+
aSlice = array[:] // equals to aSlice = array[0:10] aSlice has all elements
221+
222+
// slice from slice
223+
aSlice = array[3:7] // aSlice has elements d,e,f,g,len=4,cap=7
224+
bSlice = aSlice[1:3] // bSlice contains aSlice[1], aSlice[2], so it has elements e,f
225+
bSlice = aSlice[:3] // bSlice contains aSlice[0], aSlice[1], aSlice[2], so it has d,e,f
226+
bSlice = aSlice[0:5] // slcie could be expanded in range of cap, now bSlice contains d,e,f,g,h
227+
bSlice = aSlice[:] // bSlice has same elements as aSlice does, which are d,e,f,g
228+
229+
fmt.Println("slice ar =", ar)
230+
fmt.Println("slice a =", a)
231+
fmt.Println("slice b =", b)
232+
fmt.Println("array =", array)
233+
fmt.Println("slice aSlice =", aSlice)
234+
fmt.Println("slice bSlice =", bSlice)
235+
fmt.Println("len(bSlice) =", len(bSlice))
236+
}
237+
func show_map() {
238+
fmt.Println("show_map()")
239+
// use string as key type, int as value type, and you have to use `make` initialize it.
240+
var numbers map[string]int
241+
// another way to define map
242+
numbers = make(map[string]int)
243+
numbers["one"] = 1 // assign value by key
244+
numbers["ten"] = 10
245+
numbers["three"] = 3
246+
247+
// Initialize a map
248+
rating := map[string]float32{"C": 5, "Go": 4.5, "Python": 4.5, "C++": 2}
249+
250+
fmt.Println("map numbers =", numbers)
251+
fmt.Println("The third number is: ", numbers["three"]) // get values
252+
// It prints: The third number is: 3
253+
254+
// map has two return values. For second value, if the key doesn't exist,ok is false,true otherwise.
255+
csharpRating, ok := rating["C#"]
256+
if ok {
257+
fmt.Println("C# is in the map and its rating is ", csharpRating)
258+
} else {
259+
fmt.Println("We have no rating associated with C# in the map")
260+
}
261+
262+
delete(rating, "C") // delete element with key "c"
263+
fmt.Printf("map rating = %#v\n", rating)
264+
}
265+
func main() {
266+
show_multiple_assignments()
267+
show_bool()
268+
show_different_types()
269+
show_strings()
270+
show_string_manipulation()
271+
show_errors()
272+
show_iota()
273+
set_default_values()
274+
show_arrays()
275+
show_slices()
276+
show_map()
277+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Example code for Chapter 2.2 from "Build Web Application with Golang"
2+
// Purpose: Try to fix this program.
3+
// From the console, type `go run main.go`
4+
package main
5+
6+
func main() {
7+
var i int
8+
}

0 commit comments

Comments
 (0)