Golang Program to Show Usage of Static keyword in Class


What is Static Keyword in Golang?

The term "static" is used to describe variables, functions, and methods that have a fixed or unchanging value or behavior. In Go, the static keyword does not exist. In go we use the term "variables" to describe the memory locations where data is stored and manipulated.

Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Then, create a class name MyStruct and define a property of type int in it.

  • Step 3 − Then, we need to create a function to this variable and perform some task in it. in this example we are simply printing the value of parameter contained by it.

  • Step 4 − Now, start the main() function. Define an object to the struct created above and use “.” Notation to store data value to it.

  • Step 5 − Now, call the method created above and it will print the value of that variable.

Example 1

In this example, we will write a go language program to show the usage of static keyword in a class by creating a function to that class.

package main
import "fmt"

// creating a class
type MyStruct struct {
   staticVar int
}

// creating a function to the class
func (s MyStruct) staticMethod() {
   fmt.Println("staticVar:", s.staticVar)
}
func main() {
   // initializing an object
   var s MyStruct
   s.staticVar = 1
   s.staticMethod()
}

Output

staticVar: 1

Example 2

In this example, we will write a go language program to show the usage of static keyword in a class by creating a pointer function to that class.

package main
import "fmt"
type MyStruct struct {
   staticVar int
}

func (s *MyStruct) staticMethod() {
   fmt.Println("staticVar:", s.staticVar)
}

func main() {
   var s MyStruct
   s.staticVar = 1
   s.staticMethod()
}

Output

staticVar: 1

Example 3

In this example, we will write a go language program to show the usage of static keyword by defining a variable in the function defined to the class.

package main
import "fmt"

type MyStruct struct {
   staticVar int
}

func (s MyStruct) staticMethod() {
   s.staticVar = 2
   fmt.Println("staticVar:", s.staticVar)
}

func main() {
   var s MyStruct
   s.staticMethod()
}

Output

staticVar: 2

Example 4

In this example, we will write a go language program to show the usage of static keyword by defining a variable in a function to the class and passing the pointer to the object to it as argument.

package main
import "fmt"

type MyStruct struct {
   staticVar int
}

func (s *MyStruct) staticMethod() {
   s.staticVar = 2
   fmt.Println("staticVar:", s.staticVar)
}

func main() {
   var s MyStruct
   s.staticMethod()
}

Output

staticVar: 2

Conclusion

We have successfully compiled and executed a go language program to show the usage of static keyword in a class along with examples. In the first two examples we have used created the static variable and have assigned the value to a variable in the main() function while in the last two we have used pointer variables to the functions.

Updated on: 16-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements