github.com/searKing/golang/go@v1.2.117/exp/types/conv_test.go (about)

     1  // Copyright 2023 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package types_test
     6  
     7  import (
     8  	"fmt"
     9  	"math"
    10  
    11  	"github.com/searKing/golang/go/exp/types"
    12  )
    13  
    14  func ExampleConv() {
    15  
    16  	fmt.Printf("%#x\n", types.Conv[int8, uint8](-1))           // overflow
    17  	fmt.Printf("%d\n", types.Conv[uint8, int8](math.MaxUint8)) // overflow
    18  	fmt.Printf("%d\n", types.Conv[float32, int32](-1))
    19  	fmt.Printf("%f\n", types.Conv[int32, float32](-1))
    20  	// Output:
    21  	// 0xff
    22  	// -1
    23  	// -1
    24  	// -1.000000
    25  
    26  }
    27  
    28  func ExampleConvBinary() {
    29  
    30  	fmt.Printf("%#x\n", types.ConvBinary[int8, uint8](-1))           // overflow
    31  	fmt.Printf("%d\n", types.ConvBinary[uint8, int8](math.MaxUint8)) // overflow
    32  	fmt.Printf("%d\n", types.ConvBinary[float32, int32](-1))
    33  	fmt.Printf("%f\n", types.ConvBinary[int32, float32](-1082130432))
    34  	fmt.Printf("%d\n", types.ConvBinary[*stringerVal, int64]((*stringerVal)(nil)))
    35  	fmt.Printf("%d\n", types.ConvBinary[*stringerVal, int64](nil))
    36  	// Output:
    37  	// 0xff
    38  	// -1
    39  	// -1082130432
    40  	// -1.000000
    41  	// 0
    42  	// 0
    43  
    44  }