Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions variables/constants.go
Original file line number Diff line number Diff line change
@@ -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

}
8 changes: 8 additions & 0 deletions variables/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

func main() {
vars()
const_var()
}

// go run *.go
21 changes: 21 additions & 0 deletions variables/variables.go
Original file line number Diff line number Diff line change
@@ -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)
}