Q:

Method vs Functions in Golang, what's difference?

A:

  • Method:
  1. define the behavior of a type

  2. should use state

  3. logically connected

    For example:

    type Something struct{
        name string
    }
     
    func (s Something) Name() string {
        return s.name 
    }    
    
  • Function:
  1. should be stateless

  2. can accept interface as input

    For example:

    type Something interface {
        Name() string
    }
     
    func GetName(s Something) string {
        return s.Name()
    }
    

Ref: