Programming in Go

Speaker- SANGAM BIRADAR and Hire Me!

page visit count
Visitor Counter
العربية لوجو ديزاين السعودية

Programming in Go

Golang Series by Sangam Biradar

Tested Infrastructure

Platform Number of Instance Reading Time
Play with GO 1 5 min

Pre-requisite

Open [PWG] Platform on your browser

Chapter 1.Programming in Go

Lets Start With First Hello world - Golang

Getting Start with Golang

PWG [PWG] Platform on your browser


					
					
					
		package main
                import "fmt"
                func main() {
	        fmt.Println("Hello world!")
                }			


                               
                                
				
					

copy the above code in PWG ready Platform.

A complete program is created by linking a single, unimported package called the main package with all the packages it imports, transitively. The main package must have package name main and declare a function main that takes no arguments and returns no value



                             func main() { … }
				
					

Program execution begins by initializing the main package and then invoking the function main. When that function invocation returns, the program exits. It does not wait for other (non-main) goroutines to complete.

Go: Meaning of the ‘fmt’ package acronym fmt is short for format. Package fmt implements formatted I/O with functions analogous to C’s printf and scanf. The format ‘verbs’ are derived from C’s but are simpler.

1. Numeral Systems (Decimal) - Programming in GO

PWG [PWG] Platform on your browser


					

                                 package main

                                 import "fmt"

                                 func main() {
	                         fmt.Println(42)
                                 }
				
					

copy the above code in PWG ready Platform.

2. Numeral Systems (binary) - Programming in GO

PWG [PWG] Platform on your browser


					
package main

import "fmt"

func main() {
 fmt.printf("%d - %b \n", 42, 42)
}
				
					

copy the above code in PWG ready Platform.

in above program the annotation verb %b formats a number in binary ex:- which represent 101010 ( base 2 format) and the annotation verb %d formats a number in base 10 ex:which represent 42 ( base 10 format) integer cheatsheet for fmt:



%b	base 2
%c	the character represented by the corresponding Unicode code point
%d	base 10
%o	base 8
%q	a single-quoted character literal safely escaped with Go syntax.
%x	base 16, with lower-case letters for a-f
%X	base 16, with upper-case letters for A-F
%U	Unicode format: U+1234; same as "U+%04X"
				
					
3. numeral systems(hexadecimal) - Programming in GO

PWG [PWG] Platform on your browser


					
   package main

        import "fmt"

        func main() {
	        //	fmt.Printf("%d - %b - %x \n", 42, 42, 42)
	       //	fmt.Printf("%d - %b - %#x \n", 42, 42, 42)
	      //	fmt.Printf("%d - %b - %#X \n", 42, 42, 42)
	        fmt.Printf("%d \t %b \t %#X \n", 42, 42, 42)
       }
				
					

copy the above code in PWG ready Platform.

integer to hexadecimal cheatsheet:


					
  
%x	base 16, with lower-case letters for a-f
%x	base 16, with upper-case letters for a-f
				
					

in above program the annotation verb %x formats a number in hexadecimal Ex:- which represent 0X2A ( base 16 format)

4. numeral systems(using loop) - Programming in GO Print decimal,binary,hexadecimal number from 10 to 15 using loop

PWG [PWG] Platform on your browser


					
 package main

import "fmt"

func main() {
	for i := 1; i < 16; i++ {
		fmt.Printf("%d \t %b \t %x \n", i, i, i)
	}
}

				
					

copy the above code in PWG ready Platform.

if your new to loop in go so lets see some detailing of for loop in go:

go has only one looping construct, the for loop.

the init statement: executed before the first iteration

the condition expression: evaluated before every iteration

the post statement: executed at the end of every iteration. the init statement will often be a short variable declaration, and the variables declared there are visible only in the scope of the for statement.

the loop will stop iterating once the boolean condition evaluates to false.

note: unlike other languages like c, java, or javascript there are no parentheses surrounding the three components of the for statement and the braces { } are always required.

as we seen decimal,binary and hexadecimal programs in go , in this above program using loop you can print n number that convert number to any numeral system ( refer integer cheatsheet fmt) to convert number into different fmt format.quick using simple go program.

5. numeral systems(UTF-8)- Programming in GO

UTF 8 = Unicode Transformation Format – 8-bit UTF8 is a character encoding where is assigns 1,112,064 characters a binary number that is from 1 byte (8 bits) to 4 bytes (32 bits) long.

Why is it necessary? Well, our computers use binary to store data. So inside a computer information is a sequence of 0’s and 1’s. When you are writing a text file on your computer the computer needs to store that in binary code (in 0’s and 1’s). But what would the character 'a' represent in binary? Short answer is whatever you want it to be. That is why we have UTF8 (and ASCII before it), it provides a standard that says the letter 'a' will take the value of 01100010 in binary. It allows us to say this file is stored with the UTF8 encoding,so the binary code must be interpreted with that in mind.

PWG [PWG] Platform on your browser


					
package main

import "fmt"

func main() {
	for i := 60; i < 122; i++ {
		fmt.Printf("%d \t %b \t %x \t %q \n", i, i, i, i)
	}
}
				
					

copy the above code in PWG ready Platform.

as we seen decimal,binary and hexadecimal programs in Go , in this above UTF-8 program annotaition %q is used for UTF-8 character in coding. Note:-UTF-8 has been the dominant character encoding for the World Wide Web since 2009, and as of March 2018 accounts for 91.0% of all Web pages. The Internet Mail Consortium(IMC) recommended that all e-mail programs be able to display and create mail using UTF-8, and the W3C recommends UTF-8 as the default encoding in XML and HTML

Chapter 2.Programming in Go - starting date 2 feb 19

check out on daily morning 8.OO AM for more update or

visit to my blog