The most obvious difference is that Go doesn't have the implements keyword. All 
you have to do to implement an interface is implement the interface's methods, 
and the Go compiler will take care of the rest.

This rule leads to the empty interface, interface{}. This interface requires no 
methods to be satisfied, and as such is satisfied by all types, including the 
builtin types such as int64.

Go interfaces can be satisfied by any kind of type, not just struct types.

        type MySpecialInt int
        func (m MySpecialInt) String() string {
                return fmt.Sprintf("Hello! I'm %d!", int(m))
        }
        // MySpecialInt implements fmt.Stringer, so printing it with fmt 
functions will use the String() method

In Go, you do not have to predeclare your interface types;

        func x(y interface{ T() }) { y.T() }

is valid.

Go does not have surrounding types, so the rules about having an interface in a 
Java class mean nothing to Go. This is only really relevant in one place: In 
Java, if you have an interface method marked private, only the surrounding 
class can implement that specific method, not the entire package. In Go, the 
entire package is able to implement an unexported method in an interface, but 
other packages cannot:

        package a
        type Y interface { q() }

        package b
        type X int
        func (X) q() { ... } // X does NOT implement a.Y!

However, types in other packages CAN satisfy unexported interfaces:

        package a
        type y interface { Q() }

        package b
        type X int
        func (X) Q() { ... } // X DOES implement a.y

Go does not have generic types, protected inheritance, static members, abstract 
types, final members, or strictfp, so those annotations in Java interfaces are 
unavailable. (In Go's case, there is *only* strictfp; the specification implies 
in lots of places that IEEE 754 is required, though looking at it again I'm not 
sure if a superset of IEEE 754 is acceptable or not...)

Go interfaces may only specify methods; Java interfaces may also specify fields 
and nested types. (This makes sense; Go interfaces can apply to any type, and 
only structs have "fields", and Go does not have nested types.)

Java interfaces can provide a default implementation for methods that an 
implementing class may choose not to override. Go interfaces cannot.

Go does not have the same types of annotations that Java has, so @interface is 
meaningless. For details on Go's closest approximation, see struct tags.

Go does not have functional interfaces or interface literals.

> On Oct 8, 2016, at 9:14 AM, Fei Ding <fding...@gmail.com> wrote:
> 
> Recently I've been asked a question which is, what's the difference between 
> Golang and Java about interface?
> 
> 
> 
> I know there are some 'syntax-sugar level' differences, what I am interested 
> is anything beneath the ground, like how does Golang and Java implement 
> interface? What's the biggest difference? Which one is more efficient? Why?
> 
> 
> 
> Could anyone post blog links or source code about this topic? The only code I 
> can find is in src/runtime/iface.go, but I cannot understand it or get 
> anything useful by myself yet. Source code is better.
> 
> 
> 
> Thanks.
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com 
> <mailto:golang-nuts+unsubscr...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to