go-examples

Get Set Learn Go


Project maintained by sagar-jadhav Hosted on GitHub Pages — Theme by mattgraham

Structs

Example demonstrates structs. Structs are used to group one of more fields of logical types together. Click here to learn more

//For example we could represent a Point struct as:
type Point struct {
	x int
	y int
}

func main() {

	//Then in order to use it we need to initialize it. 
	//There are 3 ways to initialize a struct in Go.
	var c Point
	c := Point{x: 1, y: 2}

	//Once you have a struct instance 
	//you can access its fields using the dot . operator:

	fmt.Println(c.x) // 1
	c.x = 10         //updating value
	fmt.Println(c.x) // 10

}

Output

1
10
Try It Out Source Code

Contributors

« Home Page Previous « Interface Next » Stack