create a variable in go

To create a variable in Go, you need to declare it first with the 'var' keyword followed by the variable name and its data type. Then, you can either assign a value to the variable immediately or leave it uninitialized.

main.go
var age int               // variable declaration
var username string = "John"    // variable declaration with initialization
var height float64         // variable declaration
height = 6.2               // variable assignment

// shorthand declaration
weight := 180              // shorthand variable declaration with type inference
address := "123 Main St"   // shorthand variable declaration with type inference
415 chars
9 lines

In the above example, we declared variables of different data types such as 'int', 'string', and 'float64'. The shorthand declaration allows us to infer the data type from the initial value provided.

Overall, declaring and assigning variables in Go is simple and straightforward.

gistlibby LogSnag