Posts

Showing posts with the label Go lang Tutorial

Arrays in Go lang

Image
What is Array An array is the collection of similar type of elements. An array is the collection of fixed length. Array elements are stored in contiguous memory locations. All the array elements share the common name, but each element is uniquely identified their index or subscript, so the array is also called a subscript variable. Array index always starts with 0. Creating an array var arr [5]int Note:  In the above program snippet, arr is an array of 5 integers. By default, all the array elements are initialized with a default value. Default Values Data Type Default Value Int(s) 0 Float(s) 0.0 bool(s) false Objects nil Note: Following example demonstrate the default values for array elements. Example-1 package main import "fmt" func main() { var arr1 [5]int var arr2 [5]float32 var arr3 [5]string var arr4 [5]bool fmt.Pr

Decision Making in Go lang

Image
Introduction to decision making Sometimes we need to introduce a power in our program, that our program should automatically make the decision. For example:  if the temperature of the day is more than 20 degree then it's hot otherwise it's cool. To introduce this kind of power in our program, we use decision making statements as the list below... if-else statements nested if-else statements else-if ladder switch/case-control statement Let's create a program to check whether a number is even or odd. Program to check a number is even or odd in Go lang. package main import "fmt" func main() { var n int32 fmt.Print("Enter any number:") fmt.Scan(&n) if n % 2 == 0 { fmt.Println(n,"is even") } else { fmt.Println(n,"is odd") } } Output: Enter any number:55 55 is odd  Explanation: A number is even if it is divisible by 2 otherwise odd. So to che

Variable Naming & Scope of Variable

Image
Rules for Creating the variable name Variable name can contain Alphabets (A-Z/a-z), Digits (0-9) and Underscore( _ ). A variable name must start with alphabet or underscore. Spaces and special systems are not allowed. Reserved words are not allowed as a variable name. A variable name should be meaningful. Go is case sensitive language so age, Age, AGE all are treated as a different variable. Some correct variable names name father_name fatherName __AGE number1 car_modal_name is_verified intnum Some incorrect variable names father's_name father#name 2persons 0007 000Name int a#b Note There are two popular ways to create multi-word variables. player_name: Each word is separated by an underscore playerName: camel case, the first word is in small and from second word first letter of each word is capitalized. Variable Scope Scope is the range of places where a variable is allowed to be used is called variable scope.Variable Scope can

Variables in Go lang

Image
What is variable Variable is an entity which can change during the problem. For example: y = x + 5 x y 1 6 2 7 3 8 As shown in the above example, as the value of x is changing y gets changed. so in the above expression x and y are variable and 5 is a constant. In programming whenever you create a variable, a memory is reserved and a name is assigned to it. If initialized with value then binary value of that will gets stored in memory otherwise initialized with default value. Following figure shows the memory map of a variable. Creating a variable var name string = "Hello Go" In the above example, a variable is created labeled with the name of type string and initialized with value "Hello Go" . the var keyword is used to create a variable. var name = "Hello Go"  In the above code snippet, the only difference is that data type is not given, because when you initialize the variable then

Boolean in go lang

Image
Boolean (named on gorge bool) is a special 1 bit value which represents true or false. In Go lang, true and false are two reserved literals which represent Boolean values. Like some other programming language C, C++, go doesn't allow 1 at the place of true and 0 at the place of 0. Boolean values can be combined with logical operators. List of the logical operators is given below. && AND || OR ! NOT Logical operators && (AND) and || (OR) operators are used to combine two or more Boolean values/expressions. ! (Not) operator also called negation, just reverse the Boolean values. !true results false !false results true && (AND) operator returns true if all the conditions are true. || (OR) operator returns true if any one condition is true. The outcome of the combination will be based on the following truth table. Truth table for && (AND) and || (OR) operator C1 C2 C1 && C

String in golang

Image
Strings are sequence of characters with definite length and can be created by enclosing in double quotes "Hello Go" and back ticks `Hello Go`.  A string is a sequence of characters. Strings are the collection of individual bytes, where each character occupy single byte. Characters out of the ASCII characters (can say characters from other languages) occupy more than one byte. A string can be created by enclosing in double quotes "Hello Go" or backticks `Hello Go`. The difference between double quotes and backticks is that double quotes allow escape sequences like \n (newline) and \t (tab) and parses them but backticks don't. Example-1: package main import "fmt" func main( ) {         fmt.Println(len("Hello Go"))         fmt.Println("Hello Go"[1])         fmt.Println("Hello"+" "+"Go") } Output:  8 101 Hello Go   Output Explained: fmt.Println(len("Hello Go"))

Floating point Data types in Go lang

Image
As we have discussed, In our previous article Data types in go lang , In Go lang Numbers can be categorized into two broad categories. Integral Numbers Floating point Numbers We have already discussed integral numbers In this article we will discuss all floating point numbers. Floating point Numbers Floating point numbers are those which contains decimal places, for example (2.34 , -90.435 , 0.00007) Rules for floating point numbers  Floating point number can contain decimal places, For example (0.006, 2.45) Can be +ve and -ve. Floating point number can be written in two formats Fixed format , example (0.007) Exponent format , example (0.7 E -4) 0.7 E -4 is equivalent to 0.7 * 10 <sup>-4</sup> In 0.7 E -4, 0.7 (part before e) is called mantissa and -4 (part after E) is called exponent. Exponent format is used when a number is too large or too small to represent in decimal places. Floating point number can be of 32bits or 64bits in size in g

Data Types in Go language

Image
Data type defines a set of related values, describe the operations that can be done on them and define the way the stored in memory.  For example: Suppose you have a Car of VW Polo, but a car can be of any other company with some other basic and some advanced features. But if we called a name Car, then in your mind a picture is created that there is a vehicle with 4-wheels and steering. Each car has some features like the color, company, make, model etc. Types in programming work in a similar way, For example, "Hello World" is a string and every string have some length. Numbers Go Comes with different types to represent numbers, Numbers can be categorized into two broad categories... Integer The integer represents only those numbers which don't include any decimal places and can be of any positive and negative numbers. Example : {1, 3, -4, 11, 123, 534} all are integer numbers Go's integer type can be uint8, uint16, uint32, uint64, int8, int16, in

Say Hello world in Go lang

Image
Writing your first program "Hello World" package main import "fmt" func main( ) {        fmt.Print("Hello World") } Understanding the program line by line As we have discussed in the overview of go lang that Go program is organized in the various package. Here first statement package main specifies that this program will belong to the package main. Next line import "fmt" includes the predefined package fmt in your program so that you can use the functions (features) of this package in your program. Here import is the keyword in go lang use to import package(s). fmt stands for the format, fmt package has the various pre-defined function like Print, Println, Printf to print the formatted text and other methods like Scanf for reading the formatted values. Next Line defines the main() function, which is the starting point of every program. func is a keyword in go lang to define a function. Within the main() function we have writ

Installing go lang

Image
On Windows Download the latest version of go MSI installer from  https://golang.org/dl/  best suited for your system architecture. Start the installation by double-clicking on the installer and installer wizard will open as given in the following figure. For the installation follow the wizard instructions. Note that when you installing go lang using installer then it will ask the location where go will be installed as shown in the figure below. Installation directory of go lang is called GOROOT here the GOROOT is c:\go directory. After this follow the wizard instructions and complete the go lang installation. Test Go installation To Check that Go is installed correctly or not, you need to create a go workspace (a place /folder/directory in which you save you go programs) and create a sample go program and run the program using the command line. The sample program is given below... You can create a sample program by using any simple text editor like notepad, n

Overview of Go lang

Image
Go is a Open source programming language that makes it easy to build simple, reliable and efficient software. History of Go lang The Go programming language is initially started in 2007 as an internal Google project. The original designed by Robert Griesemer, Rob Pike and Ken Thomson. November 2009, Go is publicly released under the open source license. Go has an open source model and many developers over the world contribute to it with the original designers.  Overview of Go lang Go is often referred to as "Simple" programming language, a language that can be learned in a few hours if you already know another language C / C++. Go language was designed to feel familiar and to stay as simple as possible. Go language is created System programming in mind, even go can be used to create a web application using Google's App Engine . Why Go lang 1) Efficient use of 21st-century computers Computers have evolved, but programming language doesn'

Popular posts from this blog

String in golang

Inline V/S Block Level Element

Floating point Data types in Go lang

Escape Sequence | Formatted Printing | Interview Questions

Sum of two numbers

Operators in C Language| Part-4

Printing in C programming

Arrays in C Language

Program to check a year is leap year or not.

Data types in Java