You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Constants are declared using const keyword, which can be character, string, boolean or numeric values.
Constants cannot be declared using the := syntax.
Constants can be declared with or without a type in Go.
package main
import "fmt"
func main() {
const message = "You are Learning Go!"
const time = 07
const Truth = true
fmt.Println("Hello,This is Team Shiksha and", message,"which starts at",time ,"?", Truth )
}
You can also avoid using multiple const keyword and wrap it around brackets.
package main
import "fmt"
func main() {
const (message = "You are Learning Go!"
time = 07
Truth = true)
fmt.Println("Hello,This is Team Shiksha and", message,"which starts at",time ,"?", Truth )
}
Try Changing the value of 07 to 0700 or 070 and observe the difference in outputs. Can you explain why?
An untyped constant takes the type needed by its context, So When an untyped constant is assigned to a variable, the variable inherits the default type of the constant.
the expression need not be surrounded by parentheses ( ) but the braces { } are required.
package main
import (
"fmt"
"math"
)
//To Find Squareroot
func sqrt(x float64) string {
if x < 0 { //if x is less than 0
return "\nless than 0"
}
return fmt.Sprint("Square root of " , x ," is ", math.Sqrt(x))
}
func main() {
fmt.Println(sqrt(9), sqrt(-4))
}