diff --git a/en/02.1.md b/en/02.1.md index 2aab7016d..5c13e7072 100644 --- a/en/02.1.md +++ b/en/02.1.md @@ -17,27 +17,27 @@ or for python should we use 4 spaces or 6 spaces or a tab or two tabs and other While this might seem to be a shallow problem, when the codebase grows and more and more people work on the same code base it is becomes increasingly difficult to maintain the code's "beauty." We live in a world where robots can drive a car, so we shouldn't just write code, we should write elegant code. -For other languages there are many variables when it comes to writing code. Every language is good for its use case, but Go is a little special because it was designed at a company which is the very synonym of the Internet (and distributed computing). Typically in order to optimize programs, developers choose to write Java over Python and C++ over Java, but almost all available languages widely in use were written decades ago when 1GB storage was much pricier. Now storage and computing is relatively cheap and computers are getting multiples cores, but the "old languages" are not harnessing concurrency in a way that go does. It's not because those languages are bad; utilizing concurrency wasn't a relevant usecase while the older languages evolved. +For other languages there are many variables when it comes to writing code. Every language is good for its use case, but Go is a little special because it was designed at a company which is the very synonym of the Internet (and distributed computing). Typically in order to optimize programs, developers choose to write Java over Python and C++ over Java, but almost all available languages widely in use were written decades ago when 1GB storage was much pricier. Now storage and computing are relatively cheap and computers are getting multiple cores, but the "old languages" are not harnessing concurrency in a way that Go does. It's not because those languages are bad; utilizing concurrency wasn't a relevant use case while the older languages evolved. -To mitigate all the problems that Google faced with current tools, they wrote a systems language called Go which you are about to learn! There are many advantages to using golang and there are disadvantages too, for every coin has both sides. One of the significant improvements is in code formatting. Google has designed the language to avoid debates on code formatting. Go code written by anyone in the world (assuming they know and use `gofmt`) will look exactly the same. This won't seem to matter until you work in a team! Also when the company uses automated code review or some other fancy technique, the formatted code may break in other languages which don't have strict and standard formatting rules, but not in go! +To mitigate all the problems that Google faced with current tools, they wrote a systems language called Go which you are about to learn! There are many advantages to using Golang and there are disadvantages too, for every coin there are two sides. One of the significant improvements is in code formatting. Google has designed the language to avoid debates on code formatting. Go code written by anyone in the world (assuming they know and use `gofmt`) will look exactly the same. This won't seem to matter until you work in a team! Also when the company uses automated code review or some other fancy technique, the formatted code may break in other languages which don't have strict and standard formatting rules, but not in Go! Go was designed with concurrency in mind, please note that parallelism != concurrency, there is an [amazing post](https://blog.golang.org/concurrency-is-not-parallelism) by Rob Pike on the [golang blog](https://blog.golang.org/), you will find it there, it is worth a read. -Another very important change that is the concept of `GOPATH`. Gone are the days when you had to create a folder called `code` and then create workspaces for eclipse and what not. Now you have to keep one folder tree for go code which will be updated by the package manager automatically. It is also recommended to create folders with either a custom domain or the GitHub domain, for example I created a task manager using golang so I created a set of folders +Another very important change is the concept of `GOPATH`. Gone are the days when you had to create a folder called `code` and then create workspaces for eclipse and whatnot. Now you can keep one folder tree for Go code which will be updated by the package manager automatically. It is also recommended to create folders with either a custom domain or the GitHub domain, for example, I created a task manager using Golang so I created a set of folders `~/go/src/github.com/thewhitetulip/Tasks` **Note:** In *nix systems `~` stands for home directory, which is the windows equivalent of `C:\\Users\\username`. -Now the `~/go/` is the universe for the gocode in your machine. This is a significant improvement over other languages; we can store the code efficiently without hassles. While it might seem strange at first, this approach make a lot of sense than the ridiculous package names, i.e. package names generated for other languages using reverse domains. +Now the `~/go/` is the universe for the Go code on your machine. This is a significant improvement over other languages; we can store the code efficiently without hassles. While it might seem strange at first, this approach makes a lot of sense than the ridiculous package names, i.e. package names generated for other languages using reverse domains. **Note:** Along with `src` there are two folders `pkg` which is for packages and `bin` which is for binary. -This `GOPATH` advantage isn't just restricted to storing code in particular folder. When you have created five packages for your project, you don't have to import them like `"import ./db"`. Instead you can use `import "github.com/thewhitetulip/Tasks/db"` so that when executing `go get` on my repo, the `go` tool will find the package from `github.com/...` path if it wasn't downloaded initially. This standardizes a lot of screwed up things in the programming discipline. (<-- To remove and replace with actual explanation of why this is better) +This `GOPATH` advantage isn't just restricted to storing code in a particular folder. When you have created five packages for your project, you don't have to import them like `"import ./db"`. Instead you can use `import "github.com/thewhitetulip/Tasks/db"` so that when executing `go get` on my repo, the `go` tool will find the package from `github.com/...` path if it wasn't downloaded initially. This standardizes a lot of screwed up things in the programming discipline. (<-- To remove and replace with actual explanation of why this is better) -While there may be some founded complaints that go creators have ignored all language research done since the past 30yrs, you cannot create a product or a language which everyone will fall in love with. There are always some or the other use cases or constraints which the creators should consider. Considering all the advantages at least for web development I do not think any language gets close to the advantages which `go` has even if you ignore all that I said above. Go is a compiled language which means in production, you won't have to setup a `JVM` or a `virtualenv` and will instead have a single static binary! Like an icing on a cake, all the modern libraries are in the standard library, such as the `http` lib, allowing you to create webapps in golang without using a third party web framework. +While there may be some founded complaints that Go creators have ignored all language research done over the past 30yrs, you cannot create a product or a language which everyone will fall in love with. There are always some or other use cases or constraints which the creators should consider. Considering all the advantages at least for web development I do not think any language gets close to the advantages that `go` has even if you ignore all that I said above. Go is a compiled language which means in production, you won't have to setup a `JVM` or a `virtualenv` and will instead have a single static binary! Like icing on a cake, all the modern libraries are in the standard library, such as the `http` lib, allowing you to create web apps in Golang without using a third-party web framework. # 2.1 Hello, Go -Before we start building an application in Go, we need to learn how to write a simple program. You can't expect to build a building without first knowing how to build its foundation. Therefore, we are going to learn the basic syntax to run some simple programs in this section. +Before we start building an application in Go, we need to learn how to write a simple program. You can't expect to build a house without first knowing how to build its foundation. Therefore, we are going to learn the basic syntax to run some simple programs in this section. ## Program @@ -53,9 +53,9 @@ Are you ready? Let's Go! fmt.Printf("Hello, world or 你好,世界 or Καλημέρα κόσμε or こんにちは世界\n") } ``` -It prints following information. +It prints the following information. - Hello, world or 你好,世界 or Καλημέρα κόσμε or こんにちは世界 + Hello, world or 你好,世界 or Καλημέρα κόσμε or こんにちは世界 ## Explanation @@ -65,38 +65,38 @@ One thing that you should know in the first is that Go programs are composed by Every executable program has one and only one `main` package, and you need an entry function called `main` without any arguments or return values in the `main` package. -In order to print `Hello, world…`, we called a function called `Printf`. This function is coming from `fmt` package, so we import this package in the third line of source code, which is `import "fmt"` +In order to print `Hello, world…`, we called the function `Printf`. This function is coming from the `fmt` package, so we import this package on the third line of our source code, which is `import "fmt"` -The way to think about packages in Go is similar to Python, and there are some advantages: Modularity (break up your program into many modules) and reusability (every module can be reused in many programs). We just talked about concepts regarding packages, and we will make our own packages later. +The way to think about packages in Go is similar to Python, and there are some advantages: Modularity (break up your program into many modules) and reusability (every module can be reused in many programs). We just talked about concepts regarding packages, and we will create our own packages later. -On the fifth line, we use the keyword `func` to define the `main` function. The body of the function is inside of `{}`, just like C, C++ and Java. +On the fifth line, we use the keyword `func` to define the `main` function. The body of the function is inside of `{}`, just like in C, C++, and Java. -As you can see, there are no arguments. We will learn how to write functions with arguments in just a second, and you can also have functions that have no return value or have several return values. +As you can see, there are no arguments. We will learn how to write functions that take arguments in just a second. You can also have functions that have no return value or those with several return values. -On the sixth line, we called the function `Printf` which is from the package `fmt`. This was called by the syntax `.`, which is very like Python-style. +On the sixth line, we called the function `Printf` which is from the package `fmt`. This was called by the syntax `.`, which is very much Python-style. As we mentioned in chapter 1, the package's name and the name of the folder that contains that package can be different. Here the `` comes from the name in `package `, not the folder's name. -You may notice that the example above contains many non-ASCII characters. The purpose of showing this is to tell you that Go supports UTF-8 by default. You can use any UTF-8 character in your programs. +You may have also noticed that the example above contains many non-ASCII characters. The purpose of showing this is to point out the fact that Go supports UTF-8 by default. You can use any UTF-8 characters in your programs. -Each go file is in some package, and that package should be a distinct folder in the GOPATH, but main is a special package which doesn't require a `main` folder. This is one aspect which they left out for standardization! But should you choose to make a main folder then you have to ensure that you run the binary properly. Also one go code can't have more than one `main` go file. +Each Go file is in some package, and that package should be a distinct folder in the GOPATH, but `main` is a special package which doesn't require a `main` folder. This is one aspect which was left out for standardization! But should you choose to make a `main` folder then you have to ensure that you run the binary properly. Also, a complete Go source code can't have more than one `main` Go file. `~/go/src/github.com/thewhitetulip/Tasks/main $ go build` `~/go/src/github.com/thewhitetulip/Tasks $ ./main/main` -the thing here is that when your code is using some static files or something else, then you ought to run the binary from the root of the application as we see in the second line above, I am running the `main` binary *outside* the main package, sometimes you might wonder why your application isn't working then this might be one of the possible problems, please keep this in mind. +Another thing is when your code is using some static files or some other non-code asset, then you ought to run the binary from the root of the application as we see in the second line above, I am running the `main` binary *outside* the `main` package, sometimes you might wonder why your application isn't working then this might be one of the possible problems, please keep this in mind. -One thing you will notice here is that go doesn't see to use semi colons to end a statement, well, it does, just there is a minor catch, the programmer isn't expected to put semi colons, the compiler adds semi colons to the gocode when it compiles which is the reason that this (thankfully!) is a syntax error +One thing you will notice here is that Go doesn't seem to use semi-colons to end a statement, well, it does, just there is a minor catch, the programmer isn't expected to put semi-colons, the compiler adds semi- colons to the Go code when it compiles which is the reason the code below (thankfully!) leads to a syntax error ```Go func main () { } ``` -because the compiler adds a semi colon at the end of `main()` which is a syntax error and as stated above, it helps avoid religious wars, i wish they combine `vim` and `emacs` and create a universal editor which'll help save some more wars! But for now we'll learn Go. +because the compiler adds a semi-colon at the end of `main()` which is a syntax error and as stated above, it helps avoid religious wars, I wish they combine `vim` and `emacs` and create a universal editor which'll help end more wars! But for now, we'll learn Go. ## Conclusion -Go uses `package` (like modules in Python) to organize programs. The function `main.main()` (this function must be in the `main` package) is the entry point of any program. Go standardizes language and most of the programming methodology, saving time of developers which they'd have wasted in religious wars. There can be only one main package and only one main function inside a go main package. Go supports UTF-8 characters because one of the creators of Go is a creator of UTF-8, so Go has supported multiple languages from the time it was born. +Go uses `package` (like modules in Python) to organize programs. The function `main.main()` (this function must be in the `main` package) is the entry point of any program. Go standardizes language and most of its programming methodology, saving developers time that would have otherwise been wasted in religious wars. There can be only one main package and only one main function inside a Go main package. Go supports UTF-8 characters because one of the creators of Go is a creator of UTF-8, so Go has supported multiple languages from the time it was born. ## Links diff --git a/en/02.2.md b/en/02.2.md index c34bff2fe..adbefc0de 100644 --- a/en/02.2.md +++ b/en/02.2.md @@ -1,10 +1,10 @@ # 2.2 Go foundation -In this section, we are going to teach you how to define constants, variables with elementary types and some skills in Go programming. +In this section, we are going to learn how to define constants, variables with elementary types, and some skills in Go programming. ## Define variables -There are many forms of syntax that can be used to define variables in Go. +There are many ways to define variables in Go. The keyword `var` is the basic form to define variables, notice that Go puts the variable type `after` the variable name. ```Go @@ -16,7 +16,7 @@ Define multiple variables. // define three variables which types are "type" var vname1, vname2, vname3 type ``` -Define a variable with initial value. +Define a variable with an initial value. ```Go // define a variable with name “variableName”, type "type" and value "value" var variableName type = value @@ -24,12 +24,12 @@ var variableName type = value Define multiple variables with initial values. ```Go /* - Define three variables with type "type", and initialize their values. + Define three variables with the type "type", and initialize their values. vname1 is v1, vname2 is v2, vname3 is v3 */ var vname1, vname2, vname3 type = v1, v2, v3 ``` -Do you think that it's too tedious to define variables use the way above? Don't worry, because the Go team has also found +Do you think that it's too tedious to define variables using the above format? Don't worry, because the Go team has also found this to be a problem. Therefore if you want to define variables with initial values, we can just omit the variable type, so the code will look like this instead: ```Go @@ -47,9 +47,9 @@ Well, I know this is still not simple enough for you. Let's see how we fix it. */ vname1, vname2, vname3 := v1, v2, v3 ``` -Now it looks much better. Use `:=` to replace `var` and `type`, this is called a **short assignment**. It has one limitation: this form can only be used inside of a functions. You will get compile errors if you try to use it outside of function bodies. Therefore, we usually use `var` to define global variables. +Now it looks much better. Use `:=` to replace `var` and `type`, this is called a **short assignment**. It has one limitation: this form can only be used inside of a function. You will get compile errors if you try to use it outside of function bodies. Therefore, we usually use `var` to define global variables. -`_` (blank) is a special variable name. Any value that is given to it will be ignored. For example, we give `35` to `b`, and discard `34`.( ***This example just show you how it works. It looks useless here because we often use this symbol when we get function return values.*** ) +`_` (blank) is a special variable name. Any value that is given to it will be ignored. For example, we give `35` to `b`, and discard `34`.( ***This example just shows you how it works. It looks useless here because we often use this symbol when we get function return values.*** ) ```Go _, b := 34, 35 ``` @@ -82,7 +82,7 @@ const prefix = "astaxie_" ### Boolean -In Go, we use `bool` to define a variable as boolean type, the value can only be `true` or `false`, and `false` will be the default value. ( ***You cannot convert variables' type between number and boolean!*** ) +In Go, we use `bool` to define a variable as a boolean type, the value can only be `true` or `false`, and `false` will be the default value. ( ***You cannot convert variables' type between number and boolean!*** ) ```Go // sample code var isActive bool // global variable @@ -96,9 +96,9 @@ func test() { ``` ### Numerical types -Integer types include both signed and unsigned integer types. Go has `int` and `uint` at the same time, they have same length, but specific length depends on your operating system. They use 32-bit in 32-bit operating systems, and 64-bit in 64-bit operating systems. Go also has types that have specific length including `rune`, `int8`, `int16`, `int32`, `int64`, `byte`, `uint8`, `uint16`, `uint32`, `uint64`. Note that `rune` is alias of `int32` and `byte` is alias of `uint8`. +Integer types include both signed and unsigned integer types. Go has `int` and `uint` at the same time, they have the same length, but the specific length depends on your operating system. They use 32-bit in 32-bit operating systems and 64-bit in 64-bit operating systems. Go also has types that have specific lengths including `rune`, `int8`, `int16`, `int32`, `int64`, `byte`, `uint8`, `uint16`, `uint32`, `uint64`. Note that `rune` is an alias for `int32` and `byte` is an alias for `uint8`. -One important thing you should know that you cannot assign values between these types, this operation will cause compile errors. +One important thing you should know is that you cannot assign values between these types, this operation will cause compile errors. ```Go var a int8 @@ -110,7 +110,7 @@ Although int32 has a longer length than int8, and has the same type as int, you Float types have the `float32` and `float64` types and no type called `float`. The latter one is the default type if using brief statement. -That's all? No! Go supports complex numbers as well. `complex128` (with a 64-bit real and 64-bit imaginary part) is the default type, if you need a smaller type, there is one called `complex64` (with a 32-bit real and 32-bit imaginary part). Its form is `RE+IMi`, where `RE` is real part and `IM` is imaginary part, the last `i` is the imaginary number. There is a example of complex number. +That's all? No! Go supports complex numbers as well. `complex128` (with a 64-bit real and 64-bit imaginary part) is the default type, if you need a smaller type, there is one called `complex64` (with a 32-bit real and 32-bit imaginary part). Its form is `RE+IMi`, where `RE` is real part and `IM` is the imaginary part, the last `i` is the imaginary number. Below is an example of a complex number. ```Go var c complex64 = 5+5i //output: (5+5i) @@ -118,7 +118,7 @@ fmt.Printf("Value is: %v", c) ``` ### String -We just talked about how Go uses the UTF-8 character set. Strings are represented by double quotes `""` or backticks ``` `` ```. +We looked at how Go supports the UTF-8 character set in a previous section. Strings are represented by double quotes `""` or backticks ``` `` ```. ```Go // sample code var frenchHello string // basic form to define string @@ -134,7 +134,7 @@ It's impossible to change string values by index. You will get errors when you c var s string = "hello" s[0] = 'c' ``` -What if I really want to change just one character in a string? Try the following code. +What if you really want to change just one character in a string? Try the following code instead. ```Go s := "hello" c := []byte(s) // convert string to []byte type @@ -217,11 +217,11 @@ var( prefix string ) ``` -Unless you assign the value of constant is `iota`, the first value of constant in the group `const()` will be `0`. If following constants don't assign values explicitly, their values will be the same as the last one. If the value of last constant is `iota`, the values of following constants which are not assigned are `iota` also. +Unless the assigned value of a constant is `iota`, the first value of a constant in the group `const()` will be `0`. If the following constants don't assign values explicitly, their values will be the same as the last one. If the value of the last constant is `iota`, the values of the following constants which are not assigned are `iota` also. ### iota enumerate -Go has one keyword called `iota`, this keyword is to make `enum`, it begins with `0`, increased by `1`. +Go has a keyword called `iota`, this keyword is to make an `enum`, it begins with `0`, increased by `1`. ```Go const( x = iota // x == 0 @@ -239,10 +239,10 @@ const ( ``` ### Some rules -The reason that Go is concise because it has some default behaviors. +The reason that Go is concise is because it has some default behaviors. - Any variable that begins with a capital letter means it will be exported, private otherwise. -- The same rule applies for functions and constants, no `public` or `private` keyword exists in Go. +- The same rule applies to functions and constants, no `public` or `private` keyword exists in Go. ## array, slice, map @@ -260,9 +260,9 @@ arr[1] = 13 // assign value to element fmt.Printf("The first element is %d\n", arr[0]) // get element value, it returns 42 fmt.Printf("The last element is %d\n", arr[9]) -//it returns default value of 10th element in this array, which is 0 in this case. +//it returns the default value of the 10th element in this array, which is 0 in this case. ``` -Because length is a part of the array type, `[3]int` and `[4]int` are different types, so we cannot change the length of arrays. When you use arrays as arguments, functions get their copies instead of references! If you want to use references, you may want to use `slice`. We'll talk about later. +Because the length is a part of the array type, `[3]int` and `[4]int` are different types, so we cannot change the length of arrays. When you use arrays as arguments, functions get their copies instead of references! If you want to use references, you may want to use `slice`. We'll talk about that later. It's possible to use `:=` when you define arrays. ```Go @@ -274,7 +274,7 @@ b := [10]int{1, 2, 3} c := [...]int{4, 5, 6} // use `…` to replace the length parameter and Go will calculate it for you. ``` -You may want to use arrays as arrays' elements. Let's see how to do this. +You may want to create an array of arrays. Let's see how to do this. ```Go // define a two-dimensional array with 2 elements, and each element has 4 elements. doubleArray := [2][4]int{[4]int{1, 2, 3, 4}, [4]int{5, 6, 7, 8}} @@ -302,7 +302,7 @@ Then we define a `slice`, and initialize its data. slice := []byte {'a', 'b', 'c', 'd'} ``` `slice` can redefine existing slices or arrays. `slice` uses `array[i:j]` to slice, where `i` is -the start index and `j` is end index, but notice that `array[j]` will not be sliced since the length +the start index and `j` is the end index, but notice that `array[j]` will not be sliced since the length of the slice is `j-i`. ```Go // define an array with 10 elements whose types are bytes @@ -320,7 +320,7 @@ b = ar[3:5] // now 'b' has elements ar[3] and ar[4] ``` Notice the differences between `slice` and `array` when you define them. We use `[…]` to let Go -calculate length but use `[]` to define slice only. +calculate the length but use `[]` to define slice only. Their underlying data structure. @@ -332,7 +332,7 @@ slice has some convenient operations. - `slice` is 0-based, `ar[:n]` equals to `ar[0:n]` - The second index will be the length of `slice` if omitted, `ar[n:]` equals to `ar[n:len(ar)]`. -- You can use `ar[:]` to slice whole array, reasons are explained in first two statements. +- You can use `ar[:]` to slice a whole array, reasons are explained in the first two statements. More examples pertaining to `slice` ```Go