- Go Language is a cross-platform, open-source programming language used for general purposes.
- Go is fast, statically typed, compiled language that feels like a dynamic typed, interpreted language.
- Go syntax is similar to C++.
Uses of Go
- Web Development.
- Developing network-based programs.
- Developing cross-platform enterprise application
- Cloud-native development.
Ex : Hello World Program
// First.go package main import "fmt" func main() { fmt.Println("Hello Prwatech.") }
Output :
PS C:\GO_Language\String> go run First.go Hello Prwatech
Go Syntax:
- Package declaration
- Import package
- 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:
-
- 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 :
PS C:\GO_Language\String> go run str.go Hello, this is the first Go language program string
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 :
PS C:\GO_Language\String> go run str2.go Hii Prwatech... False
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 :
PS C:\GO_Language\String> go run str3.go Prwatech Prwatech Prwatech Prwatech Prwatech
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 :
PS C:\GO_Language\String> go run scan.go Input a number: 10 number: 10 1 Item(s) scanned. Error: <nil>
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 :
PS C:\GO_Language\String> go run scan2.go Enter the name :Sandeep Enter the age :23 My name is Sandeep I am 23 years old.