stringgobyteencoderksuid

ksuid genration return string in go


I am using below function to generate ksuid in byte format but i need it to return ksuid in string format.

I tried returning id.string() value but i am getting below error error: cannot use id.String() (value of type string) as id_return value in return statement.

I would like it to assign the return string value like below (string instead of byte) var id_gen string = genKsuid()

type id []byte

type id_return string

func genKsuid() id {
    id, err := ksuid.NewRandomWithTime(time.Now())
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("ksuid:     %s\n", id.String())
    return id.Bytes()
}

var id_gen []byte = genKsuid()

Solution

  • Change your function to

    func genKsuid() string {
        id, err := ksuid.NewRandomWithTime(time.Now())
        if err != nil {
            log.Fatal(err)
        }
        fmt.Printf("ksuid:     %s\n", id.String())
        return id.String()
    }