embed

package
v2.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractFile

func ExtractFile(fs *embed.FS, srcPath, dstPath string) error
Example

Extract the file from the embed path (src) to the output path (dst).

outputFilePath := "./output.txt"
if err := ExtractFile(&embedBuildBat, "testData/hello.txt", outputFilePath); err != nil {
	panic(err)
}
if _, err := os.Stat(outputFilePath); os.IsNotExist(err) {
	panic(err)
}
defer func() {
	if err := os.Remove(outputFilePath); err != nil {
		panic(err)
	}
}()
f, _ := os.Open(outputFilePath)
defer f.Close()
dataBytes, _ := io.ReadAll(f)
fmt.Println(string(dataBytes))
Output:

Hello World!

func PromiseExtractFile added in v2.3.0

func PromiseExtractFile(fs *embed.FS, srcPath, dstPath string,
	successFunc func(dst string) error,
	errFunc func(err error) error,
) (err error)

PromiseExtractFile 與ExtractFile類似,但可以在成功或者失敗的時候在做某些事情

Example
outputFilePath := "./output.txt"
if err := PromiseExtractFile(&embedBuildBat, "testData/hello.txt", outputFilePath,
	func(dst string) error {
		f, _ := os.Open(dst)
		dataBytes, _ := io.ReadAll(f)
		_ = f.Close()
		fmt.Println(string(dataBytes))
		return os.Remove(outputFilePath)
	}, nil); err != nil {
	panic(err)
}

if err := PromiseExtractFile(&embedBuildBat, "not_exist_file.txt", outputFilePath,
	nil, func(err error) error {
		fmt.Println("got an error")
		return err
	}); err == nil {
	panic(err)
}
Output:

Hello World!
got an error

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL