- Go - Home
- Go - Overview
- Go - Environment Setup
- Go - Program Structure
- Go - Basic Syntax
- Go - Data Types
- Go - Variables
- Go - Constants
- Go - Identifiers
- Go - Keywords
- Go - Operators
- Go - Arithmetic Operators
- Go - Assignment Operators
- Go - Relational Operators
- Go - Logical Operators
- Go - Bitwise Operators
- Go - Miscellaneous Operators
- Go - Operators Precedence
- Go Decision Making
- Go - Decision Making
- Go - If Statement
- Go - If Else Statement
- Go - Nested If Statements
- Go - Switch Statement
- Go - Select Statement
- Go Control Flow Statements
- Go - For Loop
- Go - Nested for Loops
- Go - Break Statement
- Go - Continue Statement
- Go - Goto Statement
- Go Functions
- Go - Functions
- Go - Call by Value
- Go - Call by Reference
- Go - Functions as Values
- Go - Function Closure
- Go - Function Method
- Go - Anonymous function
- Go Strings
- Go - Strings
- Go - String Length
- Go - String Concatenation
- Go - Compare Strings
- Go - Split String
- Go - Substring Extraction
- Go - String Replacement
- Go - String Interpolation
- Go - Parse Date Strings
- Go Arrays
- Go - Arrays
- Go - Multidimensional Arrays
- Go - Multidimensional Arrays
- Go - Passing Arrays to Functions
- Go - Pointers
- Go - Pointers
- Go - Array of pointers
- Go - Pointer to pointer
- Go - Passing pointers to functions
- Go Advanced Control Structures
- Go - Scope Rules
- Go - Dereferencing Pointer
- Go - Structures
- Go - Slice
- Go - Slice of Slices
- Go - Range
- Go - Maps
- Go - Recursion
- Go - Type Casting
- Go - Interfaces
- Go - Type Assertion
- Go - Error Handling
- Go - Concurrency
- Go - Regular Expression
- Go - Inheritance
- Go - Packages
- Go - Templates
- Go - Reflection
- Go - Generics
- Go File Handling
- Go - Read File By Word
- Go - Read File By Line
- Go - Read CSV Files
- Go - Delete File
- Go - Rename & Move File
- Go - Truncate a File
- Go - File Read-Write Mode W/O Truncation
- Go Miscellaneous
- Go - defer Keyword
- Go - Fmt Package
- Go - Zero Value
- Go - Import
Go - Method
Go programming language supports special types of functions called methods. In method declaration syntax, a "receiver" is present to represent the container of the function. This receiver can be used to call a function using "." operator. For example −
Syntax of a Method
func (variable_name variable_data_type) function_name() [return_type]{
/* function body*/
}
Example
package main
import (
"fmt"
"math"
)
/* define a circle */
type Circle struct {
x,y,radius float64
}
/* define a method for circle */
func(circle Circle) area() float64 {
return math.Pi * circle.radius * circle.radius
}
func main(){
circle := Circle{x:0, y:0, radius:5}
fmt.Printf("Circle area: %f", circle.area())
}
When the above code is compiled and executed, it produces the following result −
Circle area: 78.539816
Methods with Struct Type Receiver
You can create a method with a struct type receiver that operates on a copy of the struct, so whatever changes will be done in the method, they do not affect the original struct.
Example
package main
import "fmt"
// Struct
type Rectangle struct {
width, height float64
}
// Define a method with struct type receiver
func (rect Rectangle) Area() float64 {
return rect.width * rect.height
}
func main() {
rectObj := Rectangle{width: 2.4, height: 4.5}
fmt.Println("Area of Rectangle:", rectObj.Area())
}
When the above code is compiled and executed, it produces the following result −
Area of Rectangle: 10.799999999999999
Methods with Non-Struct Type Receiver
You can also create a method withnon-struct type receivers of such type whose definition is present in the same package.
Example
package main
import "fmt"
// Creating a custom type based on int
type value int
// Defining a method with a non-struct receiver
func (v value) cube() value {
return v * v * v
}
func main() {
x := value(3)
y := x.cube()
fmt.Println("Cube of", x, "is", y)
}
Cube of 3 is 27
Methods with Pointer Receiver
You can create a method that can havepointer receivers. This approach reflects the changes done in the method in the caller.
Example
package main
import "fmt"
type student struct {
grade string
}
// Method with pointer receiver to modify data
func (s *student) updateGrade(newGrade string) {
s.grade = newGrade
}
func main() {
s := student{grade: "B"}
fmt.Println("Before:", s.grade)
// Calling the method to update the grade
s.updateGrade("A")
fmt.Println("After:", s.grade)
}
Before: B After: A