Variable declarations and primitives
(1) Declaration using var keyword
var <variable name > <type>
var <variable name > <type>
package mainimport "fmt"func main() {var i inti = 24fmt.Println(i)}
This is used if you want to initialize a variable at a different position
Another way is
package mainimport "fmt"func main() {var number int = 52fmt.Println(number)}
(2) Implicit initialization
Here go with pick up the data type
package mainimport "fmt"func main() {number := 52fmt.Println(number)}
All local variables that are declared should be used otherwise it will throw a compilation error.
Data Types
(1) Value data types - Here the variables refer to the values the variables hold
(2) Pointer Data types - Here the variables refer to the address location where the values are stored.
Complex data type
This is a built in data type in Go.
package mainimport "fmt"func main() {a := 10.0b := 5.0c := complex(a, b)fmt.Println(c)r, i := real(c), imag(c)fmt.Println(r, i)}Output :(10+5i) 10 5
Type
int An integer. Holds whole numbers.
float64 A floating-point number. Holds numbers with a fractional part. (The 64 in the type
name is because 64 bits of data are used to hold the number. This means that
float64 values can be fairly, but not infinitely, precise before being rounded off.)
bool A Boolean value. Can only be true or false.
string A string. A series of data that usually represents text characters
No comments:
Post a Comment