diff --git a/variables/constants.go b/variables/constants.go new file mode 100644 index 0000000..fb6cd61 --- /dev/null +++ b/variables/constants.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" +) + +func const_var() { + // constant variable declaration + const str = `Constant variables in Go, where a compile time value is assigned to a variable and + cannot be changed during the execution of the program.` + fmt.Println(str) + + const PI float64 = 3.14 + fmt.Println("Value of PI:", PI) + + // PI = 3.14159 + // fmt.Println("New Value of PI:", PI) + // This will cause a compile-time error + +} diff --git a/variables/main.go b/variables/main.go new file mode 100644 index 0000000..9cfd69c --- /dev/null +++ b/variables/main.go @@ -0,0 +1,8 @@ +package main + +func main() { + vars() + const_var() +} + +// go run *.go diff --git a/variables/variables.go b/variables/variables.go new file mode 100644 index 0000000..5612630 --- /dev/null +++ b/variables/variables.go @@ -0,0 +1,21 @@ +package main + +import "fmt" + +// Different ways to declare variables in Go + +func vars() { + // Method 1: Declare variable with var keyword and specify type + var a int = 10 + // Method 2: Declare variable with var keyword without type (type inferred) + var b = 60 + // Method 3: Declare multiple variables in a single line + var c, d int = 20, 30 + // Method 4: Declare variable without initialization + var e int + e = 50 + // Method 5: Type inference | shorthand initialization + f := 40 + + fmt.Printf("a: %d b: %d c: %d d: %d e: %d f: %d\n", a, b, c, d, e, f) +}