INTRODUCTION Definition & Usage Examples
An introduction to a programming topic typically provides foundational information about the concept, its usage, and its relevance in software development. In this context, the introduction to “Definition & Usage Examples” would focus on clarifying what these terms mean in the realm of programming and how they contribute to understanding and applying programming concepts.
“Definition” refers to the explicit explanation or specification of a concept or term within the context of programming. It involves clearly defining the purpose, behavior, and characteristics of a programming construct, method, or pattern.
“Usage Examples” refer to practical demonstrations or instances showcasing how a programming concept is applied in real-world scenarios. These examples help illustrate the concept’s functionality, demonstrate its benefits, and provide insights into how it can be leveraged effectively in programming tasks.
- Go Language is a cross-platform, open-source programming language use 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.