Type parameters permit what is known as generic programming, in which functions and data structures are defined in terms of types that are specified later, when those functions and data structures are used.
String literals are those variables storing string constants that can be a single character or that can be obtained as a result of the concatenation of a sequence of characters.
Go provides two types of string literals - Raw & Interpreted string literals.
Raw string literals
`interviewbit`
Interpreted string literals
"InterviewbitWebsite"
What is the syntax used for the for loop in Golang?
// Golang program to illustrate how// to create a Closurepackage main
import"fmt"funcmain() {
// Declaring the variableGFG:=0// Assigning an anonymous// function to a variablecounter:=func() int {
GFG+=1returnGFG
}
fmt.Println(counter())
fmt.Println(counter())
}
Scan Function in GoLang
The Scan function in the Go programming language is used to read data from the standard input, format the string, and store the resultant strings into the destinations specified by the additional arguments.
package main
import (
"fmt"
)
funcmain() {
varnamestringvarunitstringvaramountintvartempstring// taking input and storing in variable using a sample input string would be:// "Faraz owns 500 acres of land"fmt.Scan(&name, &temp, &amount, &unit)
// print out new string using the extracted values fmt.Printf ("% d %s of land is owned by %s\n",amount, unit, name);
}
Format the string
In Go, the %s verb is used to format a string.
When used with a custom type that has a String() method defined, the String() method will be automatically called and its return value will be used in the formatted string.
Variadic Functions in Go
The function that is called with the varying number of arguments is known as variadic function.
One interesting thing about switch statements, is that the value after the switch keyword can be omitted, and we can have boolean conditions for each case.
age:=21switch {
caseage>20&&age<30:
// do something if age is between 20 and 30caseage==10:
// do something if age is equal to 10default:
// do something else for every other case
}