2

In go I have a named type

type identifier string

I am using a standard library method that returns []string and I want to convert that into []identifier. Is there a smoother way to do that other than:

...
stdLibStrings := re.FindAllString(myRe, -1)
identifiers := make([]identifier, len(stdLibStrings))
for i, s := range stdLibStrings {
    identifiers[i] = identifier(s)
}

My end goal is to have this named identifier type have some methods which, if I'm not mistaken, require a named type versus using a unnamed type as a receiver which isn't allowed.

Thanks.

forTruce
  • 829
  • 2
  • 7
  • 17
  • Are freestanding functions taking ordinary `string` an option? – Fred Foo Oct 31 '13 at 20:11
  • They are an option, and I realize that a function with the unnamed type argument `string` would also accept `identifier`, but I was hoping to more closely associate the functions with the type. I'm curious about type conversions like this in other cases as well. Maybe I'm not fully embracing go idiom by structuring my program like this as I've only used strong OO languages before with inheritance. – forTruce Oct 31 '13 at 20:20
  • [This](http://stackoverflow.com/questions/12753805/type-converting-slices-of-interfaces-in-go) [has](http://stackoverflow.com/questions/17848725/in-go-how-do-i-convert-mybyte-to-byte) [been](http://stackoverflow.com/questions/9121515/converting-slice-of-structs-to-slice-of-empty-interface) [asked before](http://stackoverflow.com/questions/13752819/elegant-way-to-convert-a-slice-of-one-type-to-a-slice-of-an-equivalent-type). Maybe we need a FAQ or something? – nemo Nov 01 '13 at 00:37
  • @nemo: In this question the slice memory representations are the identical, they have the same underlying type. In the other question they are not identical. Therefore, the answers are different. – peterSO Nov 01 '13 at 08:55
  • @peterSO They don't have the same underlying type. `[]identifier` is a type literal and as such its own underlying type which is not equal to `[]string`. – nemo Nov 01 '13 at 10:05
  • @nemo: See my answer to this question. – peterSO Nov 01 '13 at 12:33

1 Answers1

3

The Go Programming Language Specification

Assignability

A value x is assignable to a variable of type T ("x is assignable to T") in [this case]:

x's type V and T have identical underlying types and at least one of V or T is not a named type.

For example,

package main

import "fmt"

type Indentifier string

func (i Indentifier) Translate() string {
    return "Translate " + string(i)
}

type Identifiers []string

func main() {
    stdLibStrings := []string{"s0", "s1"}
    fmt.Printf("%v %T\n", stdLibStrings, stdLibStrings)
    identifiers := Identifiers(stdLibStrings)
    fmt.Printf("%v %T\n", identifiers, identifiers)
    for _, i := range identifiers {
        t := Indentifier(i).Translate()
        fmt.Println(t)
    }
}

Output:

[s0 s1] []string
[s0 s1] main.Identifiers
Translate s0
Translate s1
peterSO
  • 158,998
  • 31
  • 281
  • 276
  • Are you suggesting the OP should create a named type for `[]string` instead? – Fred Foo Oct 31 '13 at 22:24
  • 1
    This is slightly different in a critical way than what I wanted to accomplish. I want an `[]identifier` not `[]string` as I want to be able to use an individual `identifier` as a reciever: `func (i identifier) Translate() string`. Assuming my original copy was required would that be the standard approach to changing `[]unnamedtype` to `[]namedtype`? – forTruce Nov 01 '13 at 01:11
  • @forTruce if you require a []identifer then the way you are doing it is probably the correct way (type conversion of each slice element) – Intermernet Nov 01 '13 at 01:49
  • @forTruce: If you want a `Translate` method on `type Indentifier string`, simply add one. See my revised answer. – peterSO Nov 01 '13 at 02:53