adding minor changes
This commit is contained in:
@@ -21,13 +21,13 @@ For other languages there are many variables when it comes to writing code, ever
|
||||
|
||||
So to mitigate all the problems that Google faced with the current tools, they wrote a systems language called Go, which you are about to learn! There are many advantages to using golang, and there might be disadvantages too for every coin has both sides. But significant improvements in places like code formatting, since they designed the language in such a way that there won't be wars on how to format code, the gocode 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 then in other languages which don't have strict and standard formatting rules then the code might get screwed up, but not in go!
|
||||
|
||||
Go was designed with concurrency in mind, please note that parallelism != concurrency, there is an amazing post by Rob Pike on the golang blog, blog.golang.org, you will find it there, it is worth a read.
|
||||
Go was designed with concurrency in mind, please note that parallelism != concurrency, there is an amazing post by Rob Pike on the golang blog, [www.blog.golang.org](https://blog.golang.org/concurrency-is-not-parallelism), you will find it there, it is worth a read.
|
||||
|
||||
Another very important change that go has brought in programming that I personally love 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 and it'll be updated by the package manager automatically. Also under the code we are 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, it is just a significant improvement over other languages so we can store the code efficiently without hassles, it might seem strange at first, but it does make a lot of sense over the ridiculous package names some other languages use like reverse domains.
|
||||
|
||||
note: along with src there are two folders `pkg` which is for packages and `bin` which is for binary
|
||||
**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, but when you have created five packages for your project then you don't have to import them like `"import ./db"`, you can give it `import "github.com/thewhitetulip/Tasks/db"`, so while doing a `go get` on my repo, the `go` tool will find the package from `github.com/...` path if it wasn't downloaded initially, it just standardizes a lot of screwed up things in the programming discipline.
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ 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 brief statement. But wait, it has one limitation: this form can only be used inside of 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 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.
|
||||
|
||||
`_` (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.*** )
|
||||
```Go
|
||||
|
||||
39
en/02.3.md
39
en/02.3.md
@@ -108,7 +108,7 @@ for index := 10; index>0; index-- {
|
||||
```
|
||||
`for` can read data from `array`, `slice`, `map` and `string` when it is used together with `range`.
|
||||
```Go
|
||||
for k,v:=range map {
|
||||
for k,v := range map {
|
||||
fmt.Println("map's key:",k)
|
||||
fmt.Println("map's val:",v)
|
||||
}
|
||||
@@ -119,6 +119,14 @@ for _, v := range map{
|
||||
fmt.Println("map's val:", v)
|
||||
}
|
||||
```
|
||||
|
||||
With go you can as well create an infinite loop, which is equivalent to `while true { ... }` in other languges.
|
||||
|
||||
```GO
|
||||
for {
|
||||
// yout logic
|
||||
}
|
||||
```
|
||||
### switch
|
||||
|
||||
Sometimes you may find that you are using too many `if-else` statements to implement some logic, which may make it difficult to read and maintain in the future. This is the perfect time to use the `switch` statement to solve this problem.
|
||||
@@ -210,9 +218,9 @@ func max(a, b int) int {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
func main() {
|
||||
x := 3
|
||||
y := 4
|
||||
z := 5
|
||||
@@ -224,8 +232,8 @@ func max(a, b int) int {
|
||||
fmt.Printf("max(%d, %d) = %d\n", x, z, max_xz)
|
||||
fmt.Printf("max(%d, %d) = %d\n", y, z, max(y, z)) // call function here
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
In the above example, there are two arguments in the function `max`, their types are both `int` so the first type can be omitted. For instance, `a, b int` instead of `a int, b int`. The same rules apply for additional arguments. Notice here that `max` only has one return value, so we only need to write the type of its return value -this is the short form of writing it.
|
||||
|
||||
### Multi-value return
|
||||
@@ -392,21 +400,14 @@ import "fmt"
|
||||
type testInt func(int) bool // define a function type of variable
|
||||
|
||||
func isOdd(integer int) bool {
|
||||
if integer%2 == 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
return integer%2 != 0
|
||||
}
|
||||
|
||||
func isEven(integer int) bool {
|
||||
if integer%2 == 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
return integer%2 == 0
|
||||
}
|
||||
|
||||
// pass the function `f` as an argument to another function
|
||||
|
||||
func filter(slice []int, f testInt) []int {
|
||||
var result []int
|
||||
for _, value := range slice {
|
||||
@@ -417,16 +418,18 @@ func filter(slice []int, f testInt) []int {
|
||||
return result
|
||||
}
|
||||
|
||||
var slice = []int{1, 2, 3, 4, 5, 7}
|
||||
|
||||
func main() {
|
||||
slice := []int{1, 2, 3, 4, 5, 7}
|
||||
fmt.Println("slice = ", slice)
|
||||
odd := filter(slice, isOdd) // use function as values
|
||||
odd := filter(slice, isOdd)
|
||||
even := filter(slice, isEven)
|
||||
|
||||
fmt.Println("slice = ", slice)
|
||||
fmt.Println("Odd elements of slice are: ", odd)
|
||||
even := filter(slice, isEven)
|
||||
fmt.Println("Even elements of slice are: ", even)
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
It's very useful when we use interfaces. As you can see `testInt` is a variable that has a function as type and the returned values and arguments of `filter` are the same as those of `testInt`. Therefore, we can have complex logic in our programs, while maintaining flexibility in our code.
|
||||
|
||||
### Panic and Recover
|
||||
|
||||
Reference in New Issue
Block a user