Convert type

This is an example Go program which shows how to convert a user-defined type. In the example, MyInt means the same as int, but a direct conversion i = j is not possible.

package main

import (
        "fmt"
)

type MyInt int

func main() {
        var i int
        var j MyInt
        fmt.Println (i, j)
        i = 10
        fmt.Println (i, j)
        // j = i does not work
        j = MyInt(i)
        fmt.Println (i, j)
}

(download)

The output of the example looks like this:

0 0
10 0
10 10


Copyright © Ben Bullock 2009-2018. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer