Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Difference between method and function in GO

Posted on December 1, 2023December 1, 2023 by admin

There are some important differences between method and function. Let’s see the signature of both

Function:

func some_func_name(arguments) return_values

Method:

func (receiver receiver_type) some_func_name(arguments) return_values


From the above signature, it is clear that method has a receiver argument. A receiver can be a struct or any other type. The method will have access to the properties of the receiver and can call the receiver’s other methods.
This is the only difference between function and method, but due to it they differ in terms of functionality they offer

  • A function can be used as first-order objects and can be passed around while methods cannot.
  • Methods can be used for chaining on the receiver while function cannot be used for the same.
  • There can exist different methods with the same name with a different receiver, but there cannot exist two different functions with the same name in the same package.
  • function
  • go
  • golang
  • method
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP