Golang Basic Knowledge: Programming

0
260
Golang Basic Knowledge Programming
Golang Basic Knowledge Programming

Introduction Golang

Go is actually better known as golang. It was developed by Google in 2006 by Robert Griezmer, Rob Pike, and Kane Thompson. The language was released in 2009. It is already being used in several Google production systems.

Before we get started, we need to set up Go and learn about its built-in tools.

Installation: Golang

For those who use Mac OSX, installing using Homebrew is very easy: Golang

brew install go

Mac users using Ubuntu can install go using apt:

sudo apt install golang

Run the GO program: Golang

The Go program is very easy to run, if the name of our original Go file is main.go then we can run the program using this command in the terminal:

go run main.go 

A go program starts with the package keyword. We will learn more about packaging later. Currently we do not have the name of the package main. We did not import the fmt module after defining the package. This module contains functions related to string formatting. We can output using its Println function.

Code Sample:

package main
 import "fmt"
 func main() {
     fmt.Println("hello go")
 }

Let’s run the program:

$ go run hello.go
 hello go

Dia, we wrote our first Go program and ran it 🙂

Values: Golang

There are several types of value types in Go. Notable among these are strings, integers, floats, booleans, etc. Let’s print some basic types using the Println function seen in the previous chapter:

package main
 
import"fmt"
 
 
funcmain(){
  
 
    // We can pair two strings using `+
    fmt.Println("go" + "lang")
 
    // Integer and floating point number 
    fmt.Println("1+1 =", 1+1)
    fmt.Println("7.0/3.0 =", 7.0/3.0)
 
    // Boolean test 
    fmt.Println(true && false)
    fmt.Println(true || false)
    fmt.Println(!true)
} 

Output:

 $ go run main.go
golang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
false

Variables: Golang

Variables have to be defined explicitly. We can define one or more variables using the var. This keyword is followed by the name of the variable and then the type.

It is possible to define them all at once by separating them with multiple variable commas of the same type. Also if we imply type then guess the type from the go value. It is also possible to create variables using shorthand syntax without var keyword and type declaration.

In the following code block we will look at some examples of variables:

 package main
 
import"fmt"
 
 
funcmain(){
  
 
    // `var` We can create one or more variables
    var a string = "initial"
    fmt.Println(a)
 
    // It is possible to define many variables at once 
    var b, c int = 1, 2
    fmt.Println(b, c)
 
    // If you don't specify the type, Go guesses the type automatically
    var d = true
 
    fmt.Println(d)
 
    // If value is not given "Zero Value" is taken
    was e int
    fmt.Println(e)
 
    // Shorthand `: = without typing using syntax 
    // Variables can be defined and declared
    f := "short"
    fmt.Println(f)
} 

Constants: Golang

We use the keyword const to define constants in go. Let’s assign the name of the constant after this keyword and then its value.

Constants can be defined in all the places where we can use var.

package main
 
import"fmt"
 
 
// Constant declaration
const s string = "constant"
 
funcmain(){
  
    fmt.Println(s)
    const n = 500000000
 
    fmt.Println(n)
} 

For: Golang

In all other languages we have seen for a loop. The funny thing is that all kinds of looping are done in Go. In other words, there is no whale, do-whale, or for-it loop in Go. But we can continue to work on them quite easily!

For loop: Golang

We didn’t see our familiar for the loop at first. Here:

 for j :=7; j <=9; j++{
   
    fmt.Println(j)
} 

It is familiar to all of us for a loop. Here the value of j is given as 7 at the beginning, the loop will continue until its value is 9. Each time the value of j is increased by one at the end of the loop.

While loop: Golang

Depending on one condition in the other languages, we will do the same thing in Go, as we used to use the Whale Loop. The only difference here is that we do the while loop using the keyword.

 package main
 
import"fmt"
 
 
funcmain(){
  
 
    i :=1
 
    for i <=3{
  
        fmt.Println(i)
        i = i +1
 
    }
 
 
} 

First for and then give the original condition. Notice that no brackets were required when defining the condition for here. If only one condition is given after the for, then the loop will continue as long as that condition is true.

If we do not give any condition then it will continue in an infinite loop.

for {
    fmt.Println("infinite loop")
    break
} 

For each loop: Golang

In Go we use for loop using for loop. This requires a range function. We will, therefore, see in the range chapter how we can use it for ich.

If / Els

Like other programming languages ​​Go EFLs works. Let’s look at a simple example:

       if 7 % 2 == 0 {
    
        fmt.Println("7 is even")
    } else {
        fmt.Println("7 is odd")
    } 

As I have seen in the previous chapter, there is no need to put a condition bracket on the go. The same is true of If Els. If after the statement we write the condition directly. Brackets are not required.

If we wish, we can use any other expression we need after the if statement and before the original condition. Any variable declared here will be accessible at all branches of EFLS.

     if num :=9; num <0{
   
        fmt.Println(num,"is negative")
 
    }elseif num <10{
    
        fmt.Println(num,"has 1 digit")
 
    }else{
  
        fmt.Println(num,"has multiple digits")
 
    } 

Our main condition here is num <0 but before that we have set the value of num to 9. Notice that all the blocks but the num variable is go to be accessed.

In this example we also saw the use of else if. We use this els-ef with ef to check multiple conditions.

There is no ternary operator concept in Go, so no matter how short the logic is, you have to write the whole EFLS block, there is no shortcut.

Switch

A basic example of a switch statement not seen:

     i := 2
    fmt.Print("write ", i, " as ")
    switch i {
    case 1:
        fmt.Println("one")
    case 2:
        fmt.Println("two")
    case 3:
        fmt.Println("three")
    }

Here I give my switch statement with an expression. Then match our cases with the value of that expression to see if the conditions match. The code block runs along with the cases that match.

We can comma case statements with multiple conditions, such as:

 package main
 
import"fmt"
 
import "time"
 
funcmain(){
  
    switch time.Now().Weekday() {
    case time.Saturday, time.Sunday:
        fmt.Println("it's the weekend")
    default:
        fmt.Println("it's a weekday")
    }
} 

However, we can use dynamic expressions in case statements as well as switch statements. We cannot pass any expression in the switch statement if we want, in which case the switch can be used as if-els –

package main
 
import"fmt"
 
import "time"
 
funcmain(){
  
    t := time.Now()
    switch {
    case t.Hour() < 12:
        fmt.Println("it's before noon")
    default:
        fmt.Println("it's after noon")
    }
} 

See here case t.Hour () <12 Dynamic Expressions. And we used t.Hour () directly in the case statement, instead of passing t in the switch statement. Here’s what we could do with EFLS:

 package main
 
import "fmt"
import "time"
 
func main() {
    t := time.Now()
    if t.Hour() < 12 {
        fmt.Println("it's before noon")
    }
 
} 

#Tech24infobd #golang

LEAVE A REPLY

Please enter your comment!
Please enter your name here