-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
60 lines (51 loc) · 1.61 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package magic_test
import (
"fmt"
"github.com/kwilczynski/go-magic"
)
// This example show the basic usage of the package: Open and initialize
// the Magic library, set appropriate flags, for a given file find its
// MIME identification (as per the flag set), print the results and close
// releasing all initialized resources.
func Example_basic() {
// Open and load the default Magic database.
m, err := magic.New()
if err != nil {
panic(fmt.Sprintf("An has error occurred: %s\n", err))
}
m.SetFlags(magic.MIME)
mime, err := m.File("test/fixtures/gopher.png")
if err != nil {
panic(fmt.Sprintf("Unable to determine file MIME: %s\n", err))
}
fmt.Printf("File MIME is: %s\n", mime)
m.Close()
// Output:
// File MIME is: image/png; charset=binary
}
// func Example_must() {
// }
// This example shows how to quickly find MIME type for a file.
func ExampleFileType() {
// The magic.FileType function will open the Magic database,
// set flags to "MIME", return the result, and then close
// the Magic database afterwards.
mime, err := magic.FileType("test/fixtures/gopher.png")
if err != nil {
panic(fmt.Sprintf("An has error occurred: %s\n", err))
}
fmt.Printf("File MIME type is: %s\n", mime)
// Output:
// File MIME type is: image/png
}
// This example show how to identify encoding type of a buffer.
func ExampleBufferEncoding() {
buffer := []byte("Hello, 世界")
mime, err := magic.BufferEncoding(buffer)
if err != nil {
panic(fmt.Sprintf("An has error occurred: %s\n", err))
}
fmt.Printf("Data in the buffer is encoded as: %s\n", mime)
// Output:
// Data in the buffer is encoded as: utf-8
}