Go programming language The Ultimate Beginners Guide to Learn Go Programming Step by Step (John Bach, Alexander Aronowitz) (Z-Library)

Author: John Bach, Alexander Aronowitz

GO

No Description

📄 File Format: PDF
💾 File Size: 494.4 KB
48
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
(This page has no text content)
📄 Page 2
Go programming language The Ultimate Beginner's Guide to Learn Go Programming Step by Step By John Bach 3 nd Edition
📄 Page 3
"Programming isn't about what you know; it's about what you can figure out . ” - Chris Pine Memlnc.com Introduction …………………..………….………5
📄 Page 4
Chapter I General concepts……………………….………6 Chapter II Understanding go………………………..…….19 Chapter III Writing the first program………………...….…..34 Chapter IV the rules……………………………………….…..53 Chapter VI Create Backend…………………..………………84 Introduction You may have heard in the last few years about a new programming language that originated from within Google called Go (or Golang as a searchable term for search engines), through this book we will try to identify this language, its advantages, disadvantages and what makes it different from others. The first chapter of this book will be a verbal lesson only, focusing on the points of difference of language with the rest of the languages, and is directed to those with some programming background with the rest of the languages, but the rest of the lessons will be directed to beginners.
📄 Page 5
Chapter One General concepts As usual every decade, there are new tools and techniques that try to take advantage of the mistakes of their ancestors from tools and techniques. And "What is the need for a new programming language?" Or "Why don't they agree on a single programming language?", A programming language is just a tool for your tasks, each of which is more appropriate for a task. Choose for each task the most appropriate tool, and there is no need to sanctify the tool and the establishment of war wars - non-technical - around them or create an imaginary obsession prevents learning. A team of programmers from within Google felt it was time to improve their workflow in C and ++ C, and they needed a new tool that eliminated the flaws of those two, further improved their productivity, and was relevant to the quality of Google's computational needs. They founded the Go programming language within Google in 2007. The long established team of the language includes Unix's founder, Ken Thompson (a computer science, Ken) who worked for Bell Labs, and Robert Pike, who was also part of Bell's Unix team. Labs, one of the founders of the UTF-8 codec, and finally Robert Griesemer is a V8 JavaScript engine worker. Knowing this simple historical overview of Go, I like to kill you all overly enthusiastic, I don't like you to love programming language because of its founders, or it came out of a company you love, so I will start to list things you may not like about the programming language Go, even if you did not like it , You might want to stop reading. I would be personally happy to see very few readers of this series really knowing what they are doing, rather than a momentum enthusiastic about everything new without awareness and knowledge of things. Things you may not like about the Go programming language
📄 Page 6
1. Object-oriented is not If you think it is, there is no concept of Class or Objects and therefore there is no inheritance. However, Go carries some of the advantages of object-oriented programming such as the provision of Interface, Structural Functions, and Struct composition. Why? The founders of language believe that object-oriented languages carry many flaws and complexities that can be eliminated by abandoning some of these concepts altogether. Even Java programmers themselves recommend Composition, for example, more often than inheritance, language founders who believe that object-oriented programming is often a bad idea, technically. 2. No Exception Handling handled This may be a corollary due to the absence of Go's concept of object programming. Go errors are handled in a fairly traditional way, since errors are returned as normal error values. Where error is a primitive type in itself as any other native type (int, string .. etc). However, Go allows you to throw an error for exceptional cases via the keyword panic (similar to raise or throw in other languages) as well as recover from these errors by recover. 3. No default or optional arguments can be passed to functions (default / optional arguments) In other programming languages, you might be used to doing something like:
📄 Page 7
function listFolders (path, subfolders = false, recursive = false) {...} In Go, however, you cannot pass subfolder = false nor recursive = false as a sign to the listFolders function because it will not accept such default / optional parameters, resulting in a compile error. Why? Language founders believe that these behaviors contribute to building unstable APIs or make their behavior unpredictable. In our previous example, for example, they prefer to write the function without default operators, ie: func listFolders (path string, subfolders bool, recursive bool) {...} This forces you to write the behavior you want from the function explicitly instead of letting the code interface dictate you to act as the default, this to reduce human errors. This may also prompt you to write three functions, each with its own distinct behavior, for example: listFolders, listFoldersRecursivly and listFoldersWithFirstLevelSubFolders. 4. No Overloading feature For the same reasons, there is no Method overloading, ie you cannot redefine a function with the same name but with a different signature. For example, if there is a function named: func listFolders (path string) {...} You cannot create another function with the same name but with a different signature such as: func listFolder (path string, level int) {...} You should change its name to:
📄 Page 8
dunc listFolderToLevel (path string, level int) {...} However, there is an indirect way to make a function accept arbitrary values by making the {} interface type signature we will look at in the next lessons. 5. No generics Other languages enable you to write generic functions or classes, where you do not declare the type of parameters you accept but leave them to know the type of parameters later when you call them. A type to be determined, so <List> String or <List> Integer can be easily created while maintaining the same functions and operations that can be performed on the List in general. There is no Go like this, and instead there is a {} interface as a universal type that satisfies all types, but it is not a perfect alternative to Generics. Why? The reason for the lack of Generics in Go is that its founders have not yet figured out the most appropriate way for them to add this feature to the language without increasing load during run-time. 6. Go is a boring language and is not the best programming language! Because of the simplicity of language and nothing new, many consider it a boring language. The number of keywords and their original types is small compared to other languages, and they greatly reduce the existence of more than one way of doing a task. It doesn't even have a while loop and is limited to a for loop. Many consider this a feature of language, but I mentioned it to you so you don't expect anything new to show off.
📄 Page 9
Also, the language will not allow you to leave a variable without using or importing an unused import / variable and the compiler will never accept it. 7. Stubborn language The founders of language are firm in their opinion and decisions in the design of the language, do not expect significant changes that may occur in the short or medium term in the language or changes in the way things do and the behavior of the compiler. There is no need to open blank discussions about language design and its flaws unless you are at the same level of experience and wisdom. They themselves declare this, and remember that there are other options and programming languages if you don't like Go. 8. There is no consensus on a single package manager Python has pip, javascript has npm, and other languages have a popular or agreed package manager, Go is not without a package manager, it has a lot of it, but it has not yet agreed on a single package manager or how to get and list the dependencies In a standard way, but recently it is done through the concept of Vendoring. These eight things, for the male and not limited to the harshest criticism of Go as a programming language, if you agree it may not suit you language, and if you feel that behind it wisdom - like me - continue reading the chapter on things you may like about Go. Things You May Like About the programming language Go 1. Light parallel threads (goroutines)
📄 Page 10
Go allows you to run parallel processing and simply take full advantage of your CPU, as it has the so-called goroutine, a parallel thread that is lighter than thread in other languages. This allows you to release tens of thousands of parallel threads (instead of just tens or hundreds of threads) without having to create them and make them communicate or complement each other. It is enough, for example, to create a function that works in a thread alone by going with the keyword go, for example: go func () {// A nasty function that does things in a thread alone .... } This allows writing high-performance applications much easier. 2. Fast compiled language Unlike Ruby, Python, Java and PHP, Go is an assembly language. The final output of your program will be a stand-alone executable containing all the dependencies needed to run it without the need for external dependencies. This is very useful in web development, for example, to eliminate or reduce dependency problems. ). This feature also facilitates the deployment of your application (deployment) where you will have to just transfer the executable to the server for example, then run it and enough. No need to install anything else to run your software, no need to even install Go on the server itself. It may come to mind that compiling can take time, especially if the size of the program is large, but the fact that Go is very fast and often you won't even notice it. 3. Productive language
📄 Page 11
Because it's a boring language, it narrows you to argue with your co- worker or someone working with you on the project or looking for the best-amazing way to do something, everything is simple and clear. 4. Multiple platforms par excellence The same software can be reassembled to run on Windows, Linux, Mac OSx or even smartphones and ARM architecture. Even after version 1.5 of Go you can create an executable file for all platforms through the platform you are in, for example through the Linux system can create an executable file for Windows and Mac OS without the need to be on that system. 5. Rich Standard Library Despite the novelty of language, its standard library has made progress in a standard circumstance. It is often advised to rely on the standard library as much as possible, as it contains many things needed by most types of applications, and even has an HTML template engine, tools for dealing with json encoding, encryption of passwords and other usable things in web applications, for example. 6. Unicode everything by default The subject of encoding strings and how to handle the character digitally is great, but Go has made everything unicode by default unless otherwise specified explicitly. Go has a rune type instead of a char type in the rest of the languages, which is simply an abbreviation (alias) for the int32 type that only points to the Unicode encoding point for that character. So the string in Go is a rune string, not a char string that refers to byte as in C, for example. 7. Good built-in tools
📄 Page 12
The documentation of your software can be generated by simply typing a godoc command, improving the formatting by typing gofmt, or starting code testing procedures (for example, unit testing) by typing go test, all tools that come with the Go installation. 8. Easy language in terms of writing and formatting (Syntax) The format of the language is somewhat similar to Python, where there is no semicolon after the end of each instruction (;), and a new variable can be made without specifying its type. : = 35 The int type will be assigned to the age variable. Also, functions can return more than one value, such as in Python. Perhaps the most different thing to other languages is the method of writing functions, since the value returned by the function is written to the right of the function (as in Pascal) and not to the left, for example: func HelloHsoub () string { return "Hello Hsoub Academy!" } Note that string is written to the right of the function, not to the left of it, that is, it returns a value of type string. Functions can also return other functions as a value. Variables can also refer to functions (such as in JavaScript, Python, etc.). sayHi: = HelloHsoub So as not to take sides, these are 8 things you might like.
📄 Page 13
Chapter II Understanding go In the previous chapter, we learned about Go and its differences with other programming languages. In this chapter, we will try to explain how to install it, configure and understand its workspace. It is very important to understand the folder partitioning that the Go environment dictates to you. Go has two types of compilers. As a quick reminder, the compiler's job is to take your code in its purely textual form, and turn it into an executable (or library) program. One of the Go compilers is the standard compiler named gc for Go Compiler, and the other is a compiler that relies on the gcc compiler and what it provides as a platform and support for more CPU architectures called gccgo. It should be noted that the gccgo complex is somewhat late in implanting the full specifications of the newer version of Go. For example, in writing this lesson, gccgo provides version 1.4.1 of Go, while gc always provides the latest version (1.5.2) of the language. I mentioned this for the sake of knowing, in short, if you are not interested in supporting more architectures or some details of the
📄 Page 14
gcc platform you will not need gccgo and you will be satisfied with the standard gc which we will follow throughout this series. Install Go As mentioned in the previous lesson, Go is a multi-platform language par excellence, ready to download and install on your preferred system for both 32bit and 64bit architectures. Install Go on Windows and Mac OS Simply go to the language site and then download the version of your system. Its installation process is normal and is similar to the installation of any other regular program. After installation, make sure the operation was successful by typing go version or just go on the command line. If you're on Windows, make sure to close any previously opened command-line window (if any) and then open a new window. Install Go on Linux I advise you to check your package manager before downloading and installing Go from anywhere else. If you find that your distribution package manager provides the latest version of Go, it will be much easier. If you are on Ubuntu and the distributions based on it, you probably think that the Go version provided by the apt package manager is outdated, since the distribution is updated every 6 months or two years (if it is LTS). So I advise you to install by adding an etherneum repository. sudo add-apt-repository -y ppa: ethereum / ethereum sudo apt-get update sudo apt-get install golang
📄 Page 15
The rest of the distributions are supposed to contain the latest version of Go, then it is enough to install from the package manager, for example on Arch Linux: sudo pacman -S go After installation, make sure that the installation was successful by typing go version or just go on the command line. You should receive a similar result to this: go version go1.5.2 linux / amd64 Understand the work environment The working environment here is two basic: A folder on your computer, created anywhere you want, will be a place for all Go projects. An environment variable called GOPATH refers to this folder. Suppose you name the work folder as work, which should also contain three other subfolders: A sub-folder called src, where you will place all the code for all of your Go projects. A subfolder named bin, where the gc aggregator will transfer executables generated by your code directly to that folder. A subfolder named pkg, such as bin but not for executables, if you write a program that is a library and not an executable program itself, or fetch an external library via the go get command, it will be compiled as a library that is placed in that folder so that it can be easily fetched in other programs without reuse. Compile them each time (this speeds up the process of compiling your executable that uses these libraries).
📄 Page 16
Given the above, the Go environment will be similar to this tree division: work folder ├ ── bin # The resulting executables folder Kg pkg # The resulting program libraries folder └── src # Our code folder Is that all? of course not. Since Go is a relatively recent language, it assumes that you will host your code in a place (repository), which is usually any professional programmer, fearing that the code will be lost or shared with other people. Of course, your code repository will be managed with a version management tool (VCS) such as Git or Mercurial. The code may be on your own server, or on a software hosting service such as Github and Bitbucket. Since Github is the most popular platform for sharing code, I'll give it an example. Go prompts you to place your code in a path similar to your github account, for example, and even close the image. Suppose your github account is called HsoubAcademy and your account link is: https://github.com/HsoubAcademy If you create a Go project named example for example and then upload it to Github using your HsoubAcademy account, the project link is: https://github.com/HsoubAcademy/example
📄 Page 17
Go will prompt you to have the project folder on your computer similar to this link, ie under this path: work / src / github.com / HsoubAcademy / example Where: github.com is a subfolder under the src folder. HsoubAcademy is a subfolder under the above github.com folder, for example, change it to your Github account. example is a subfolder that contains your project files under the HsoubAcademy folder. Note: Folder names are case-sensitive except for Windows. Why arrange the work environment like this? Simply because organization in this way is very useful in two ways: 1. Any external library can be fetched by simply importing it from its repository path. For example, suppose that we want to use a library to decompress the Arabic text, we will use the goarabic library, which will be enough to include in our program. go get github.com/01walid/goarabic 2. The go get command places the output of this library in the pkg folder with the same path as its link on github, ie it will be placed in a
📄 Page 18
subfolder named 01walid inside the github.com folder which is a subfolder within the working folder, so it can be included directly in our project. the shape: import "github.com/01walid/goarabic" Note that the path to import this library is the same as its link on github. This makes it easy to share code libraries and enrich the language libraries themselves. It is very important that your project follows the same curriculum, especially if it is a library. Configuring the work environment For Linux or Mac OS, for example, you can create a folder named work inside your Home folder by typing a command: mkdir $ HOME / work Then make the latter a Go work environment by creating an environment variable named GOPATH (case sensitive) that indicates: export GOPATH = $ HOME / work Of course to make this variable permanent even after rebooting, you will have to type it in bashrc. (If you are using a bash shell, or a zshrc file. If you are using a zsh shell, for example, files that are inside your home folder HOME $). If you're on Windows, just add a variable called GOPATH by going to: System Properties (System -> Advanced system settings) Then click on Environment Variables
📄 Page 19
Then add the variable from the System variables section as shown below. For example, if your work folder in Windows is C: \ Projects \ Go, you should add it like this: If the selected path for your work folder is shown, you have successfully installed and configured Go. Bonus
📄 Page 20
What if we can make the executables we write or download via the go get command available directly to us in the command line window without browsing them every time? For example, let's say we want to use the ha tool written in Go, to convert Markdown files into HTML ready to be pasted into another editor, we bring the tool: go get github.com/HsoubAcademy/ha Since the ha tool is an executable program, the go get command will move it directly to the bin folder within the work environment. But we don't want to navigate to the bin folder inside the working folder to use it, just want to type ha in the command line directly to take advantage of the tool and use it even if we are in a free folder. Since Go places the output of the executables in the bin folder under the working folder, it is enough to add the bin folder path to the PATH environment variable paths so that you have everything! Even your own executive programs you write yourself. For Linux or Mac OS users, this is possible by adding the bin folder path within the working folder to the PATH environment variable: export PATH = $ PATH: $ GOPATH / bin For Windows users, add the bin folder path within the working folder to the PATH environment variable by going to: System Properties (System -> Advanced system settings) Then click on Environment Variables Find the Path value in the System variables section and press edit and add the path to the end of the power preceded by a semicolon (;) as shown below.
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List