github.com/roboticscm/goman@v0.0.0-20210203095141-87c07b4a0a55/doc/progs/json2.go (about)

     1  // cmpout
     2  
     3  // Copyright 2012 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package main
     8  
     9  import (
    10  	"fmt"
    11  	"math"
    12  )
    13  
    14  func InterfaceExample() {
    15  	var i interface{}
    16  	i = "a string"
    17  	i = 2011
    18  	i = 2.777
    19  
    20  	// STOP OMIT
    21  
    22  	r := i.(float64)
    23  	fmt.Println("the circle's area", math.Pi*r*r)
    24  
    25  	// STOP OMIT
    26  
    27  	switch v := i.(type) {
    28  	case int:
    29  		fmt.Println("twice i is", v*2)
    30  	case float64:
    31  		fmt.Println("the reciprocal of i is", 1/v)
    32  	case string:
    33  		h := len(v) / 2
    34  		fmt.Println("i swapped by halves is", v[h:]+v[:h])
    35  	default:
    36  		// i isn't one of the types above
    37  	}
    38  
    39  	// STOP OMIT
    40  }
    41  
    42  func main() {
    43  	InterfaceExample()
    44  }