Introduction :

  • date 2nd August, 2022 |
  • by Prwatech |
  • 0 Comments
  1. Go Language is a cross-platform, open-source programming language used for general purposes.
  2. Go is fast, statically typed, compiled language that feels like a dynamic typed, interpreted language.
  3. Go syntax is similar to C++.

Uses of Go

  1. Web Development.
  2. Developing network-based programs.
  3. Developing cross-platform enterprise application
  4. Cloud-native development.

 

Ex : Hello World Program

// First.go
package main
import "fmt"
func main() {
    fmt.Println("Hello Prwatech.")
}

Output :

Go Syntax:

  1. Package declaration
  2. Import package
  3. Statements and expressions
package main         				//Package declaration
import "fmt"					//Import Package 
func main() {
    fmt.Println("This is  Prwatech Institute of Data Science")	//Statement and expressions
}

 

 

 

    • Strings in Golang:

  1. In this program we have to write a string in a sentence then print the sentence and data type.
//str.go
package main
import (
    "fmt"
    "reflect"
)
func main() {
    var s string = "Hello, this is the first Go language program."
    fmt.Println(s)
    fmt.Println(reflect.TypeOf(s))
}

 

Output :

2) Go program to illustrate how to check the given string start with the specified prefix

//str2.go
package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "Hii Prwatech..."
    fmt.Println(s)
    fmt.Println(strings.HasSuffix(s, "BI"))
}

Output :

3) Go program to illustrate how to repeat a string to a specific number of times

//str3.go
package main

import (
    "fmt"
    "strings"
)

func main() {
    var str = "Prwatech "
    fmt.Println("\n ", strings.Repeat(str, 5))
}

Output :

4) Golang program to demonstrate the example of fmt.Scan() function.

package main

import 
    "fmt"
func main() {
    var no int
    // Input a number
    fmt.Print("Input a number: ")
    n, err := fmt.Scan(&no)

    // Print the value, n and err
    fmt.Println("number: ", no)
    fmt.Println(n, " Item(s) scanned.")
    fmt.Println("Error: ", err)
}

Output :

5) Golang program to demonstrate the example of fmt.Scan() function. Input two values & write their Sentence.

package main
import "fmt"

func main() {
    var name string
    var age int

    fmt.Println("Enter the name :")
    fmt.Scan(&name)
    fmt.Println("Enter the age  :")
    fmt.Scan(&age)
    fmt.Printf("My name is  %s I am  %d years old.", name, age)

}

Output :

Quick Support

image image