File tree Expand file tree Collapse file tree 7 files changed +127
-2
lines changed Expand file tree Collapse file tree 7 files changed +127
-2
lines changed Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "fmt"
5+ "runtime"
6+ )
7+
8+ const maxHeapSize = 2 * 1024 * 1024 * 1024 // 1GB
9+
10+ func runGC () {
11+ fmt .Println ("--- Demonstrating GC ---" )
12+ // create 2 gb of junk memory
13+ var junk = make ([]byte , maxHeapSize )
14+ fmt .Println (junk [:10 ])
15+
16+ // run gc manually on the allocated
17+ fmt .Println ("Running GC" )
18+ runtime .GC ()
19+ fmt .Println ("\n GC Finished" )
20+ }
Original file line number Diff line number Diff line change 1+ module datatypes
2+
3+ go 1.20
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import "fmt"
4+
5+ func variableInitialization () {
6+ fmt .Println ("--- Demonstrating Variable Initialization ---" )
7+
8+ // Golang forbids unused variables
9+ // uncomment following line to give compile-time error
10+ // var unused_var string
11+
12+ // Standard declaration: var variableName dataType
13+ var age int = 20 // reassigning values to the variable
14+
15+ // Type inference: variableName := value
16+ name := "John"
17+
18+ // Multiple variable declaration: var ( variable1 dataType1 variable2 dataType2 )
19+ var height , weight float64
20+
21+ // Short declaration for multiple variables: variable1, variable2 := value1, value2
22+ country , population := "INDIA" , 142.03
23+
24+ // Printing the values of the variables
25+ fmt .Println ("Age:" , age )
26+ fmt .Println ("Name:" , name )
27+ fmt .Println ("Height:" , height )
28+ fmt .Println ("Weight:" , weight )
29+ fmt .Println ("Country:" , country )
30+ fmt .Println ("Population:" , population )
31+
32+ }
Original file line number Diff line number Diff line change 1+ package main
2+
3+ func main () {
4+ variableInitialization ()
5+ printVariables ()
6+ runGC ()
7+ pointers ()
8+ }
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "fmt"
5+ )
6+
7+ func pointers () {
8+ fmt .Println ("--- Demonstrating Pointer Variables ---" )
9+ // Create an array of integers
10+ array := [3 ]int {1 , 2 , 3 }
11+ fmt .Println (array )
12+
13+ // Create a pointer to the array
14+ pointer := & array
15+
16+ // Modify the second element of the array using the pointer
17+ // * is used to dereference the pointer
18+ (* pointer )[1 ] = 10
19+
20+ // Print the modified array
21+ fmt .Println (array )
22+
23+ // allocate new int type pointer
24+ num := new (int )
25+ * num = 10
26+ fmt .Println (num , * num )
27+ }
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import "fmt"
4+
5+ // you cannot use short hand := initialization on global scope
6+ var globalVariable = 10
7+
8+ func printVariables () {
9+ fmt .Println ("--- Demonstrating Variable Scopes ---" )
10+ localVariable := 5
11+
12+ // Print global and local variables within main function
13+ fmt .Println ("Global:" , globalVariable )
14+ fmt .Println ("Local (main):" , localVariable )
15+
16+ {
17+ // Declare local variables within compound block
18+ localVariable := 7
19+ anotherLocalVariable := 15
20+ globalVariable := 20
21+
22+ // Print local and global variables within compound block
23+ fmt .Println ("Local (block):" , localVariable )
24+ fmt .Println ("Another local (block):" , anotherLocalVariable )
25+ fmt .Println ("Global (block):" , globalVariable )
26+ }
27+
28+ // Print local and global variables after the compound block
29+ fmt .Println ("Local (main, after block):" , localVariable )
30+ fmt .Println ("Global (main, after block):" , globalVariable )
31+ }
Original file line number Diff line number Diff line change 11go 1.20
22
3- use ./funcname
4- use ./helloworld
3+
4+ use (
5+ ./funcname
6+ ./datatypes
7+ ./helloworld
8+ )
You can’t perform that action at this time.
0 commit comments