1. 嵌入文件:
    1. 嵌入为字符串:
      package main
      import (
      _ "embed"
      "fmt"
      )
      //go:embed test.txt
      var s string
      func main() {
      fmt.Println(s)
      }
    2. 嵌入为字节数组:
      package main
      import (
      _ "embed"
      "fmt"
      )
      //go:embed test.txt
      var b []byte
      func main() {
      fmt.Println(b)
      }
    3. 嵌入为文件:
      package main
      import (
      "embed"
      "fmt"
      )
      //go:embed test.txt
      var f embed.FS
      func main() {
      data, _ := f.ReadFile("test.txt")
      fmt.Println(string(data))
      }
    4. 嵌入多个文件(通配符):
      package main
      import (
      "embed"
      "fmt"
      )
      //go:embed tmp/a/*.txt
      var f embed.FS
      func main() {
      data, _ := f.ReadFile("tmp/a/a.txt")
      fmt.Println(string(data))
      }
  2. 嵌入文件夹:
    package main
    import (
     "embed"
     "fmt"
    )
    //go:embed tmp
    var f embed.FS
    func main() {
     data, _ := f.ReadFile("tmp/a/b.txt")
     fmt.Println(string(data))
    }
文档更新时间: 2021-11-26 10:59   作者:lee