将字符串转换为Foo(字符串类型)

This example will be a little bare to strip out the custom xml parsing that I'm doing, but I've run into this issue:

package main

import (
    "encoding/xml"
    "fmt"
)

type Foo string

func main() {
    var f Foo
    var b string

    c := xml.CharData{}
    f = string(c)
    b = string(c)
    fmt.Println(b)
}
//prog.go:15: cannot use string(c) (type string) as type Foo in assignment

Foo is a type of string, what am I missing to convert the string representation of xml.CharData (which is valid, use it in many decoders) to a custom type which is a string?

Convert c to Foo directly.

f = Foo(c)

Playground: http://play.golang.org/p/WR7gCHm9El

Edit: This works because Foo is a string underneath. Foo is a new and distinct derived type; its base type is string. You can similarly make derived types for any existing type. Each derived type is distinct, so you get type safety. Conversions must be explicit.