#what is the difference between these two keywords ?

11 messages · Page 1 of 1 (latest)

true rock
#

what is the difference between using any or interface as a base type in golang ?

#
package main

import "fmt"

func main(){
    var fname any = "hello world"
    var sname interface{} = "hello world"

    fmt.Println(fname)
    fmt.Println(sname)
}
jolly sun
#

interface is a keyword

#

any is a builtin

#

any is defined as type any = interface{} which is a type alias and is in absolutely all ways equal to interface{}

true rock
#

so instead of using interface i can use any instead ?

jolly sun
#

instead of using an empty interface you can use any instead

#

you can't write:

type I any{
 SomeMethod()
}

that does not work

jovial carbon
#

interface - a keyword for defining interfaces
interface{} - an interface without any requirements, so all types implement it
any - a type alias of interface{} so same as interface{}

true rock