Skip to content

Grammar improvements #1189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions en/02.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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 `<pkgName>.<funcName>`, 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 `<pkgName>.<funcName>`, 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 `<pkgName>` comes from the name in `package <pkgName>`, 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

Expand Down
Loading