Skip to content

Instantly share code, notes, and snippets.

@Aurora12
Last active August 5, 2025 13:54
Show Gist options
  • Select an option

  • Save Aurora12/bc8725c4f9bdf44f892de9a04421a5ff to your computer and use it in GitHub Desktop.

Select an option

Save Aurora12/bc8725c4f9bdf44f892de9a04421a5ff to your computer and use it in GitHub Desktop.
// https://go.dev/play/p/zMefNMTZCOo
package main
import "fmt"
type A interface {
Test(value int) string
}
type B struct {
C string
}
func (b B) Test(value int) string {
return b.C
}
func main() {
var a1 *A
var a2 A
var b B = B{C: "asdasd"}
fmt.Printf("a1 %T %v\n", a1, a1)
fmt.Printf("a2 %T %v\n", a2, a2)
fmt.Printf("b %T %v\n", b, b)
if a1 == nil {
fmt.Println("a1 is nil")
} else {
fmt.Println("a1 is not nil")
}
if a2 == nil {
fmt.Println("a2 is nil")
} else {
fmt.Println("a2 is not nil")
}
var c1 A = &b
fmt.Printf("c1 %T %v\n", c1, c1)
var c2 A = b
fmt.Printf("c2 %T %v\n", c2, c2)
// Pointer to a struct is typed as interface:
// this gives it a typed value which is not nil.
var d *B
var a3 A = d
fmt.Printf("a3 %T %v\n", a3, a3)
if a3 == nil {
fmt.Println("a3 is nil")
} else {
fmt.Println("a3 IS NOT NIL")
}
a4 := func() A {
var b *B
return b
}()
if a4 == nil {
fmt.Println("a4 is nil")
} else {
fmt.Println("a4 IS NOT NIL")
}
var a5 A = func() *B { return nil }()
if a5 == nil {
fmt.Println("a5 is nil")
} else {
fmt.Println("a5 IS NOT NIL")
}
var a6 A = (*B)(nil)
if a6 == nil {
fmt.Println("a6 is nil")
} else {
fmt.Println("a6 IS NOT NIL")
}
}
// OUTPUT
/*
a1 *main.A <nil>
a2 <nil> <nil>
b main.B {asdasd}
a1 is nil
a2 is nil
c1 *main.B &{asdasd}
c2 main.B {asdasd}
a3 *main.B <nil>
a3 IS NOT NIL
a4 IS NOT NIL
a5 IS NOT NIL
a6 IS NOT NIL
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment