From 892af78ca6223f92c49be0d5a05da2f5d45be72d Mon Sep 17 00:00:00 2001 From: dhanakoti-shanmugasundaram Date: Thu, 18 Dec 2025 16:12:01 +0530 Subject: [PATCH 1/2] dk: added variables examples --- variables/variables.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 variables/variables.go diff --git a/variables/variables.go b/variables/variables.go new file mode 100644 index 0000000..1f44c11 --- /dev/null +++ b/variables/variables.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +// Variables declaration + +func main() { + var a int = 10 + + var b, c int = 20, 30 + + d := 40 + + var e int + e = 50 + + var f = 60 + + fmt.Println("a:", a) + fmt.Println("b:", b) + fmt.Println("c:", c) + fmt.Println("d:", d) + fmt.Println("e:", e) + fmt.Println("f:", f) +} From ba7ffb8aa89d1cdeddad14f50ba05bf113899735 Mon Sep 17 00:00:00 2001 From: dhanakoti-shanmugasundaram Date: Thu, 18 Dec 2025 18:20:12 +0530 Subject: [PATCH 2/2] dk: variables examples --- variables/constants.go | 20 ++++++++++++++++++++ variables/main.go | 8 ++++++++ variables/variables.go | 26 +++++++++++--------------- 3 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 variables/constants.go create mode 100644 variables/main.go 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 index 1f44c11..5612630 100644 --- a/variables/variables.go +++ b/variables/variables.go @@ -2,24 +2,20 @@ package main import "fmt" -// Variables declaration +// Different ways to declare variables in Go -func main() { +func vars() { + // Method 1: Declare variable with var keyword and specify type var a int = 10 - - var b, c int = 20, 30 - - d := 40 - + // 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 - var f = 60 - - fmt.Println("a:", a) - fmt.Println("b:", b) - fmt.Println("c:", c) - fmt.Println("d:", d) - fmt.Println("e:", e) - fmt.Println("f:", f) + fmt.Printf("a: %d b: %d c: %d d: %d e: %d f: %d\n", a, b, c, d, e, f) }