On Fri, Sep 11, 2020 at 9:45 AM Kevin Chadwick <m8il1i...@gmail.com> wrote: > > I apologise if this has already been discussed. Google didn't turn up anything > directly related. > > If you read a file using the following that returns a byte slice. > > tlsCertb, err := ioutil.ReadFile("/etc/ssl/mycert") > if err != nil { > log.Fatal(err) > } > tlsCert = string(tlsCertb) > > Is there a way to get a string without the cast. > > Otherwise couldn't the language automatically return a string rather than a > byte > slice in these cases if the receiving var is already a string? > > e.g. > > var string tlsCert > tlsCert, err = ioutil.ReadFile("/etc/ssl/mycert") > if err != nil { > log.Fatal(err) > }
The way Go works, ioutil.ReadFile is compiled with the io/ioutil package. It can't change based on how it is called. So it is always going to return []byte. The only way to implement what you suggest would be to add an implicit conversion from []byte to string. But that seems problematic. In general Go avoids implicit conversions except to interface types. And a conversion to string does require a copy, so it doesn't seem like a great idea to do that implicitly. If this happens a lot in your code, it seems easy enough to use a tiny helper function. Ian -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcXD9ao9g3_B3zEnaZeRLkYHPjHYWHasWPUXb6ozNA1k0g%40mail.gmail.com.