String()用法

package main

import(
    "fmt"
)

type State int
const (
    Running State = iota
    Stopped
    Rebooting
    Terminated
)
func main() {
    state := Running

    // print: "state 0"
    fmt.Println("state ", state)
}

func (s State) String() string {
    switch s {
    case Running:
        return "Running"
    case Stopped:
        return "Stopped"
    case Rebooting:
        return "Rebooting"
    case Terminated:
        return "Terminated"
    default:
        return "Unknown"
    }
}

你可能感兴趣的:(String()用法)