Golang is static, so any method should static always

Hi,
just understand myself, as we all knew any function attached to a particular type T and then it become method; and this method should be accessible by a particular type T values/objects. that mean this method become static to particular type T, and it won’t work for other types right?

if yes, and if we want to have other type T2 then need to have same method with different type T2 attached; don’t we see code reusability losing? or we have to have duplicate code since GOLAN is static.
please help, I’m missing anything.

Thanks,
Subbareddy Jangalapalli

nope, you got wrong, go does not have the concept of “static” that Java has.

Drop the idea of Java completely, and OO. Forget everything you know, because it is all wrong pertaining to Go.

What do you mean by static? Do you mean static method dispatch or static methods, like Jarrod said?

If you mean static method dispatch, then yes, functions are dispatched statically for concrete types. However, if a value is “wrapped” into an interface, then the functions are dispatched virtually.

If you mean static methods, then Jarrod is correct that there are no static methods in Go. Instead of a static method, just write a function.

Thank you, i got it. I was playing with below example, and thinking why speak method should be defined twice for each and every data type that need. Now I got it, speak can be wrapped into an interface to behave based value. thank you.

package main

import “fmt”

type person1 struct {
fname string
lname string
age int
}
type secretAgent struct {
person1
ltk bool
}

func (s secretAgent)speak() {
fmt.Println(“I’m in Method:\t”, s.fname,s.lname,s.age,s.ltk)
}
func (s person1)speak() {
fmt.Println(“I’m in Method:\t”, s.fname,s.lname,s.age)
}
func main() {
sa1:=secretAgent{person1:person1{“sr”,“jangas”,42},ltk:true}
fmt.Println(sa1)
sa1.speak()
sa2:=secretAgent{person1:person1{“Kshrugal”,“jangalapalli”,13},ltk:false}
fmt.Println(sa2)
sa2.speak()

}