Share E-Book

GO LANG For beginners (michael redcar)(Z-Library)

Author

Rating No ratings yet

Log in to rate

Go
Language English

No Description

Format PDF
Size 234.6 KB
24
Views
(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
  Sommario The Go Programming Language How to install GO in your computer Data types in GO Go Functions What are GO operators? The IF Statement in GO Programming Language Switch Statements in Go Programming Language Arrays in GO Go Programming Language - Code Testing     The Go Programming Language GO, also called as Golang is a programming language backed by Google. It promises speed, scalability, simplicity and it is a potential rival to many popular languages such as Java, C, Python, etc. It has a long way to go before it becomes as ubiquitous as Java, but it has seen very fast growth in the recent times. Many innovative tech startups and cloud computing platforms are adopting Go for their businesses. Even a bank start-up in the UK has adopted the language for its banking ledger system and other operations. Go offers high performance, quick prototyping, and fast development speed. It aims to bridge the gap between dynamic and compiled languages. It retains the development speed of dynamic languages while offering the excellent performance of the compiled languages. What is Google's Go?
Page 3
GO is an open source programming language created by Ken Thompson, Rob Pike and Robert Griesemer in 2007. Go follows the tradition of the C language, but offers many improvements in simplicity, safety, and conciseness. With GO, even large binaries compile very quickly. GO allows you to move very fast and a typical build feels instantaneous with it. GO was developed at Google when they wanted a language that is fast and which works well with automated code review. They wanted a language which could allow their large teams to work on large scale projects efficiently and cater to their networking and multi-core requirements. What is GO good for? Multi-processor devices, powerful smartphones, IoT(Internet of Things) revolution and AI(Artificial Intelligence) and other aspects of evolving technology make it necessary for the programming languages to evolve to cater to the needs of programmers efficiently. - Power Today, multi-processing executing environments make it necessary for the applications to execute multiple processes simultaneously. Go offers a powerful concurrency model by combining communication pipelines called 'channels' with light-weight threads called 'goroutines.' - Garbage Collection
Page 4
Go's built-in garbage collection solves a difficult problem of manual memory management. With Go, the cost of garbage collection is low, and it does not affect the performance of the application. - Focused Library Go has condensed the number of keywords developers are required to remember. Go has around 25 keywords as compared to more than 50 in Java. - Included Batteries Applications rely on testing for the error-free code. Go development tools brings testing right at the core of the development process. - Simplified Scoping Go offers simple conventions and three levels of scoping. Simplified scoping gives a lot of new opportunities to the developers. - Web Servers and Networks Go is particularly suitable for the development of the web applications and servers that rely on concurrency and distributed functions. The executables run much faster with Go as compared to other languages such as Python.
Page 5
- Artificial Intelligence and Machine Learning Machine Learning developers can use Google's Go platform for their projects as it has a growing number of projects to choose from. Go's toolkits take advantage of the conveniences of the language. For instance, GoLearn is a 'battery-included' machine learning library. Another example is 'Goml' which bills itself as 'Golang Machine Learning On Wire.' It includes models that let you learn online in a reactive manner. Channels hold the streams where the data is passed. Who Should Learn Go? Any developer, experienced or new, who wants to build dynamic web applications will be able to build powerful applications with Go. Web applications are the future of the Internet. Go offers a powerful and highly-efficient server side development environment for developers. As Google backs go, developers can expect a growing community especially after witnessing the rapid adoption of the language in the recent past. Any developer who wants to build powerful server side applications that may or may not involve machine learning can benefit greatly by learning the Google's Go programming language. What Do You Need To Learn Go?
Page 6
If you are already familiar with programming and concepts of C, you can benefit from them, but it is not a requirement as such. Go follows the C-language traditions, but it is not required to know C before you learn 'Go'. You only need a passion for developing powerful applications and willingness to put in a lot of time and effort into the learning process. To Sum Up Go programming language is backed by Google, and it is being adopted widely for the benefits it offers. It is perfect for server side development as it brings many powerful features at the table.   How to install GO in your computer The first step to install this programming language environment in your computer is heading to the GO download page. You can access it by navigating to https://golang.org/dl/. There, you can find different versions for various operating systems. Pick the version for your operating system and download it. Upon completion of the download, install the program. If you are using Apple Macintosh OSX operating system, GO will be installed in “/usr/local/go”. If you are using a Windows machine then it will be installed at c:\go while Linux users will find it at $HOME/go. After installing the program, open a terminal or a Command Prompt (CMD) session. After that, simply type “go env”. This will start up the program and you can see some lines of code appearing in the window.
Page 7
After installing GO, you need to create a workspace for your projects. This is some space in your hard drive which you can use to load and manipulate GO code. It is important to ensure that you only create a single workspace. This keeps things simple. As a rule of thumb, the workspace cannot be in the same folder where you installed GO. You need to create a new folder named “src” in another location of your hard drive so as to create it. If you are using an Apple Macintosh or Linux computer, you can establish a workspace by creating folders named “code/go/src”. After doing this, make sure to update the “.bash_profile”. If you are using a Windows computer, head to your $HOME folder and create directories named “code\go\src”. After that, head to your system settings and add a “GOPATH” variable. Proceed to update the “PATH” as well. After creating a workspace, you can validate it by starting up a CMD session or terminal and typing, “go env”. It should start up and indicate a line of code showing the location of the “GOPATH”. This shows that the workspace is ready and GO is successfully installed. The first steps with GO interface You can code in GO programming language in two ways. Firstly, you can decide to use regular, plain text editors. Secondly, you can use Integrated Development Environments (IDEs). The plain text editors use a command-line interface while the IDEs use a Graphical User Interface (GUI). For convenience and improved user-friendliness, it is better to use an IDE. There are many IDEs that you can use to write GO programs. Examples of these are Gogland by JetBrains, Sublime Text, Visual Studio Code and Atom IDEs. Each type has its own advantages and
Page 8
disadvantages. Visual Studio Code and Atom IDEs require you to install separate plugins before you can compile GO code. To get a GO interface, simply download one of these IDEs and install it. After that, start it up. Proceed to point it to your GO workspace so that it can save your work there. Once this is done, you are ready to develop programs in GO. For easy management of the files in your project, the Visual Studio Code and Gogland IDEs are the best. They provide you with a window that you can use to arrange your code hierarchically. They also help you to view your code and navigate it easily. The interface provided by these IDEs allows you to search through GO code through single words or language-specific constructs. By using an IDE interface to develop GO programs, you have features such as automatic code imports, automated code completion, linter integration, automated formatting of code, navigation of code material and lots more. Conclusion The latest version of GO programming language is known as GO 1.9 and was released on 24th August 2017. It has a variety of changes made to elements such as runtime, tooling, language and the standard library. It also has a major development known as type aliases. Programs written in GO can run on any operating system or computing platform. As such, Golang is one of the best programming languages today.  
Page 9
Data types in GO Newcomers at Go language programming may wonder and scratch their head in consideration of these kinds of systems; however, the Go types are just straightforward and simple. Furthermore, the grounds for understanding the key concepts and characteristics assists programmers lower moments regarding adopting the Go language. There are several languages types, which may be useful for current practitioners, and GO language has Go types data, which are represented in many forms as compared to Java and C++ it contains references types and primitive. It presents the types in Numeric, Boolean, rune, and strings. a) Boolean Boolean Go programming language can be kept in bool type. From the bool type, it was given and assigned pre-declared values, which are either true or false. When programming with Boolean type, the zero (0) values is said to be false. The declared variable assigns and enabled type bool with the initial value is true. b) Strings These are immutable text values, which are utf-8, encoded so that they represent Unicode characters. An empty string is a zero value, which is set as uninitialized meaning that it has references. Furthermore, the string values may become initialised with text literal that is surrounded by the double quotes. Likewise, these strings may be surrounded with a back quote and with the raw multi-line unencrypted text c) Rune Rune is simple integers values which are mapped to their Unicode code point, for instance, rule literal an’ as from real value 97. Also, it is characterised, equivalent and alias to int32. It is enclosed, and one or more expressed character in single quotes and in any quote they may appear any character except unescaped and newline quotes. Moreover, a single quote would stand for the Unicode value of a given character whereas, multi- character will sequence the start through backslash encodes values. A simpler rune will represent a single character in a quote because the source texts are Unicode, which has encoded characters. d) Numeric Types
Page 10
A numeric type represents the floating point and set integers that are pre- declared in architectures and the independent numeric types. The n-bits and n-integers are represented by two complement arithmetic’s that are a pre- declared set of numeric types on the implementation of specific sizes. To avoid such issue as portability, all the numeric are different except byte that is an alias of unit 8, and similarly a rune that is an alias of int32. The conversion of numeric types requires mixed expression and assignments. Conclusion From the above concept on Go programming language, the new comers or beginners may be able to understand the rule, which is involved in the computer programming system. There is a wider view of built and composite types, which enable one to see the content requires in programming. Most details are learnt in a full course in programming studies, which compiles all the left out concept and programming skills. Also, it is critical as a programmer to learn the basic programming skills so that he/she may gain the experience in performing the computing process and procedures. Finally, this course is helpful to the student because makes them create more software easily and faster in the technology industry.  
Page 11
Go Functions The Go project was a project lead Google programmers Rob Pike, Robert Griesemer and Ken Thompson with the aim of creating a computing language slightly based on C programming language. It designed to eliminate the extraneous load of languages such as C++ resulting to a language that includes various features borrowed from modern languages like pointer arithmetic, type inheritance, and method and operator overloading. Golang has not been used as free-form language; instead, it specifies a series of formatting details such as the indention and spaces to be used. Go requires that all its declared variables or imported libraries are used, and all the return statements are compulsory. Golang uses “type interference” when declaring variables. The variable type, instead of being just a part of the declaration statement, is inferred by a value type itself. For instance, in C programming language, the integer x could be set to a 0 value as represented below: Int x = 0 However, in Go, the equivalent statement would be represented as follows: X : = 0 A Go variable will be written as an integer based on the value itself. Principal Functions in GO A function also known as a subroutine or a procedure is defined as an independent section of a code that maps zero or more inputs parameters. Go function can take zero or more arguments. This is the first Go function func main() {} Function 2 The second Go function calculates the average series of set numbers. Getting average is a typical problem that is ideal for learning candidates. For the average function, will take into consideration float64 and return one float64. This should be inserted before the main function: func average(xs []float64) float64 { panic("Not Implemented") }
Page 12
Functions will start with the keyword “func,” followed by the name of the function. Inputs of the function are represented in this form (name type, name type). The function contains one parameter named “xs.” After the inputs, we can insert the return type. Collectively, the return type and the parameters are referred to as the “function’s signature.” Finally, there is the function body that has a series of statements in between the curly braces. The statements invokes built in function called “panic” that causes a run time error. Functions can be challenging at first when trying to implement them, therefore, it is advisable to break it down to manageable chunks. Taking all this into account our function for average would look like this: func average(xs []float64) float64 { total := 0.0 for _, v := range xs { total += v } return total / float64(len(xs)) } · Returning Multiple Values Go language has the ability to return numerous values from a set function: Take for instance this function: func f() (int, int) { return 5, 6 } func main() { x, y := f() } There are three changes that are necessary here: a change in the return type to include multiple lines that will be separated by comas (,). Secondly we have to change the expression after the return to accommodate multiple expressions separated by comas too. Finally we have to change the assignment statement in order to get the multiple values on the left of the: := or =. · Variadic Function
Page 13
For the last parameter of a Go function there is a special available form: func add(args ...int) int { total := 0 for _, v := range args { total += v } return total } func main() { fmt.Println(add(1,2,3)) } Through the use of “…” before the type name of the last input, you are able to show that it takes zero or more of the parameters. For this purpose, we take zero or more as “int” or “ints.” We declare the function just like in any other function although in this case we are able to pass as many “ints” as possible. Other Go functions include: · Closure – That can be represented in this form func main() { add := func(x, y int) int { return x + y } fmt.Println(add(1,1)) } · Recursion – Is a function capable of calling itself. Below is how we can computethe factorial of any number: func factorial(x uint) uint { if x == 0 { return 1 } return x * factorial(x-1) }
Page 14
(This page has no text content)
Page 15
What are GO operators?   An operator is a symbol in programming which tells the computer to perform arithmetic or logical operations. GO programing language has a wide variety of built-in operators. They include:   • Arithmetic operators   • Relational operators   • Bitwise operators   • Logical operators   • Miscellaneous operators   • Assignment operators   Arithmetic operators   The GO programming language supports various arithmetic operators. The first of these is (+) which adds up two or more operands. It also supports the (-) operator which subtracts the second operand from the first one. You can find the (*) and (/) operands too. The former one multiplies operands while the latter divides the numerator by the denominator. Another arithmetic operator that is contained in this programming language is the modulus (%) which gives a remainder after division involving integers. The language also has an increment operator (++) that increases integer values by one. The opposite of this GO operator is the (- -) which reduces and integer value by one.  
Page 16
Relational operators   These are GO operators which perform operand comparisons. They are often used in in conditional statements. They include the (= =) operator which checks if the values of 2 operands are equivalent or not. If they are, the condition that they are used in becomes true. Another relational operator in the GO programming language is (!=). This one checks if the values of two operands are equal. If not, the condition becomes true. Two other relational operands in this programming language are (<) and (>). They indicate whether an operand is larger or smaller in value than the other. In addition to those, the GO programming language has the (<=) an (>=) operators. They indicate that a value is less or larger than the other while being concurrently equal too.   Bitwise operators   These are operators in the GO programming language which work on bits. They perform bit by bit operations. These operators are organized into truth tables. Bitwise operators interact with information in form of bits such as 0 and 1.   Logical operators   These operators compare and describe operands. They represent conditions such as “AND”, “OR” and “NOT”. One of the logical operators in the programming language is (&&) which represents “AND”. As long as both operands where this operator is applied are non-zero, the condition is true. Another logical operator is the (||). This one represents “OR”. If any of the operands where this operator is applied is non-zero, then the overall
Page 17
condition is true. The (!) is a logical operator which means “NOT”. It is used to reverse the logical state of the attached operand. If a condition is true in the GO programming language, then the (!) reverses it to untrue.   Miscellaneous operators   The GO programming language also contains some miscellaneous operators. They include (&) and (*). These operators represent conditions such as “sizeof” and “?”. The (&) operator returns the address of a variable and the (*) operator points to a variable.   Assignment operators   These are used to combine logical and arithmetic operations. An example of an assignment operator is (=) which assigns values from the left to the right side operand. Another example is the (+=) operator. This one adds up the right and left operand. After that, it assigns the value to the left operand. The (-=) operator does the opposite of this. Another assignment operator found in the GO programming language include (*=) and (/=). They perform their respective arithmetic operations and then assign the resultant value to the left operand.   Operator precedence   In GO programming, there is a concept known as operator precedence. This is a guideline for the grouping of the terms in an expression. Thanks to this concept, some operators have higher precedence than others. For example, the multiplication operator (*) has higher precedence than the addition operator (+).
Page 18
  Conclusion   GO operators help us to perform logical and arithmetic operations in the GO programming language. They are comprehensively described above. By using these elements, you can create efficient programs for a wide range of application.  
Page 19
The IF Statement in GO Programming Language Go, the next-generation, open-source programming language created for the purpose of building efficient, fast, and reliable systems, web, and other applications uses 25 simple keywords in its coding. These keywords, which are even less in number than alphabets in English Language, are used along with data types; such as integers, strings, and Booleans to create codes in GoLang (Go programming language) and form the basis of control statements and function operations. This, in other words, is called flow control. From Python to Go, flow control is considered to be the greatest invention in programming, as flow control allows us to use simple control statements which can be applied in Go to represent complex logic. It has been noted that among these statements, the most common is the "IF" statement. Now, it must be known that control statements, also known as conditional statements, are part of every programming language used. With conditional statements, we can execute a code depending on the condition of the program. For example, if we are looking to run a code that runs only sometimes, but at other time does not run, then conditional statements come in handy. This indicates that with the use of conditional statements, like the “If” statement in Go, programs are able to determine whether certain conditions are being met and what should be done if they are, along with what to do in case the condition is not being met. The keyword “if”, which is likely to be the most commonly used keyword in your program, has conditional statements created around it which can be used in situations such as: · If the student earned over 60% marks in her examination results, report that her grade is pass; if not, report that her grade was fail. · If she has the money in her account, charge the expenses; if she does not, charge a penalty fee. Conditional codes, including the “if” statement in Go programming language, are therefore written in order to evaluate conditions and assign codes to run, based on whether the mentioned conditions were true or false, met or not. “If” statements are also used as simple statements in succession, as they are often used to initialize a local variable due to their acceptance of an initialization statement.
Page 20
The use of “if” as an initialization statement is said to be the most useful task concerning “if” in GoLang. With initialization statements, we can declare variables in the same line as an if-statement. Also, the scope of the variable that is being defined in the initialization statement has to be available inside the block code of the defining “if”. This simple syntax leads to clean, understandable code. And the initialization scope associated with it indicates that when we declare a variable at the start of an if- statement, it is scoped to that if-statement and any else-if and else statements involved. If we use the variable outside its scope, then it will lead to the occurrence of an "undefined" error. Which is also referred to as a compile-time error. Furthermore, “if” statements in Go are expressions that do not need a parentheses to surround them, but they do require curly braces. In Go programming language, use of the curly braces is mandatory even if there is merely one statement defined between the braces. Therefore, the syntax of the “if” statement is as follows: If condition defined with an open curly brace, followed by the condition itself which ends with a closed curly brace. Also, “If” can even begin with short statements in GoLang to be implemented before the condition. As hinted at before, in Go programming language, an "if" statement usually precedes an "else" statement, which follows only when the Boolean expression is false. However, an “else” statement following an “if” statement is optional and is not a necessary requirement. But, in the presence of “else” and “ifs”, any number of the conditional statements can be used in order to evaluate truth from the top of the code to the bottom. These if-else statements can therefore be used for multiple conditions and help in constructing complex conditions. For Go programmers, it is useful to know that “if” is, more often than not, used commonly in coding and is a recurring keyword. Therefore, basic knowledge regarding the "if" keyword and its uses can be used as a means to get a hang of the Go programming language.  
The above is a preview of the first 20 pages. Register to read the complete e-book.

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay
← Back to List