github.com/goplus/igop@v0.25.0/testdata/embed/embed.go (about)

     1  package main
     2  
     3  import (
     4  	"embed"
     5  	"fmt"
     6  	"reflect"
     7  )
     8  
     9  //go:embed testdata/data1.txt
    10  var data1 string
    11  
    12  var (
    13  	//go:embed testdata/data2.txt
    14  	data2 []byte
    15  	//go:embed testdata/*
    16  	fs embed.FS
    17  )
    18  
    19  var (
    20  	//go:embed "testdata/data1.txt"
    21  	helloT []T
    22  	//go:embed "testdata/data1.txt"
    23  	helloUint8 []uint8
    24  	//go:embed "testdata/data1.txt"
    25  	helloEUint8 []EmbedUint8
    26  	//go:embed "testdata/data1.txt"
    27  	helloBytes EmbedBytes
    28  	//go:embed "testdata/data1.txt"
    29  	helloString EmbedString
    30  )
    31  
    32  type T byte
    33  type EmbedUint8 uint8
    34  type EmbedBytes []byte
    35  type EmbedString string
    36  
    37  func checkAliases() {
    38  	want := []byte("hello data1")
    39  	check := func(g interface{}) {
    40  		got := reflect.ValueOf(g)
    41  		for i := 0; i < got.Len(); i++ {
    42  			if byte(got.Index(i).Uint()) != want[i] {
    43  				panic(fmt.Errorf("got %v want %v", got.Bytes(), want))
    44  			}
    45  		}
    46  	}
    47  	check(helloT)
    48  	check(helloUint8)
    49  	check(helloEUint8)
    50  	check(helloBytes)
    51  	check(helloString)
    52  }
    53  
    54  func checkEmbed() {
    55  	if data1 != "hello data1" {
    56  		panic(data1)
    57  	}
    58  	if string(data2) != "hello data2" {
    59  		panic(data2)
    60  	}
    61  	data, err := fs.ReadFile("testdata/sub/data2.txt")
    62  	if err != nil {
    63  		panic(err)
    64  	}
    65  	if string(data) != "sub data2" {
    66  		panic(data)
    67  	}
    68  }
    69  
    70  func main() {
    71  	checkEmbed()
    72  	checkAliases()
    73  }