Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is T in this code

Tags:

go

What really T is in this piece of code? recursive deceleration?

package main

import "fmt"

type T func() T

func main() {
    var a T
    a = func() T {
        return a
    }

    fmt.Printf("%#v", a)
}

http://play.golang.org/p/zt4CBXgrmI

Edit: I have been using Go for more than a year.

like image 788
Oguz Bilgic Avatar asked Jan 12 '23 19:01

Oguz Bilgic


1 Answers

It looks like a function type. In the declaration, T is a parameterless function that returns a T, so a function that returns a function. That is the type declaration. a is of this type T.

a is a function that returns itself, so these lines basically all do the same:

fmt.Printf("%#v", a)
fmt.Printf("%#v", a())
fmt.Printf("%#v", a()()()()())

I can't think of a good use for this, but then again, I'm far from experienced in Go.

like image 119
GolezTrol Avatar answered Jan 30 '23 20:01

GolezTrol