String or *string

i have a func that return (string, error)
and if there is an error inside the func i am returning "", err
is that good or i should change it to return (*string, err)
and to return nil, err

1 Like

Up to you whether you want to make use of nil. However, the latter (string pointer) is very uncommon so it might confuse people. Normally as a practice, whenever err != nil, we discards the use of that value regardless whether it is valid or otherwise.

Unlike other data type, string is always re-created so there isn’t a point to use string pointer except variable manipulation for function’s pass as pointer.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.