Skip to content

Commit e2efa4c

Browse files
committed
added example for library usage
1 parent 6d6cd26 commit e2efa4c

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

โ€ŽREADME.md

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ go build ecoji.go
113113
./ecoji --help
114114
```
115115
116+
For an example of how to use Ecoji as library see [library-example.md](docs/library-example.md).
117+
116118
[emoji]: https://unicode.org/emoji/
117119
[video]: https://www.youtube.com/watch?v=XCsL89YtqCs
118120
[tour]: https://tour.golang.org/welcome/1

โ€Ždocs/library-example.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Using Ecoji as a library
2+
3+
This is an example that shows how to use Ecoji as a library. Start off with a new directory.
4+
5+
```bash
6+
mkdir /tmp/ecoji-example
7+
cd /tmp/ecoji-example
8+
```
9+
10+
Then create a file called `go.mod` in that directory with the following contents.
11+
12+
```
13+
go 1.18
14+
15+
module local/ecoji-example
16+
17+
require github.com/keith-turner/ecoji/v2 v2.0.0
18+
19+
```
20+
21+
Then create file named `ecoji-example.go` with the following contents. This example program will read a file, encode with Ecoji V2, and write that to a new file.
22+
23+
```go
24+
package main
25+
26+
import (
27+
"bufio"
28+
"fmt"
29+
"github.com/keith-turner/ecoji/v2"
30+
"log"
31+
"os"
32+
)
33+
34+
func main() {
35+
36+
if len(os.Args) != 3 {
37+
fmt.Printf("Usage : %s <input file> <output file>\n", os.Args[0])
38+
os.Exit(1)
39+
}
40+
41+
input := os.Args[1]
42+
output := os.Args[2]
43+
44+
infile, err1 := os.OpenFile(input, os.O_RDONLY, 0)
45+
if err1 != nil {
46+
log.Fatal(err1)
47+
}
48+
49+
outfile, err2 := os.Create(output)
50+
if err2 != nil {
51+
log.Fatal(err2)
52+
}
53+
54+
outbuf := bufio.NewWriter(outfile)
55+
56+
//encode data using Ecoji V2 with a 72 emoji wrap
57+
if err := ecoji.EncodeV2(bufio.NewReader(infile), outbuf, 72); err != nil {
58+
log.Fatal(err)
59+
}
60+
61+
infile.Close()
62+
outbuf.Flush()
63+
outfile.Close()
64+
}
65+
```
66+
67+
Then run the following commands to build the executable.
68+
69+
```
70+
go mod download github.com/keith-turner/ecoji/v2
71+
go build ecoji-example.go
72+
```
73+
74+
Now you should be able to use the executable to encode a file. Below is an example if running it.
75+
76+
```
77+
/tmp/ecoji-example$ ./ecoji-example go.mod test.ecoji
78+
/tmp/ecoji-example$ cat test.ecoji
79+
๐Ÿ‘ฎ๐Ÿ˜ฝโ™๐Ÿงง๐ŸŽŒ๐Ÿคญ๐Ÿชœ๐Ÿ•‹๐Ÿ’Ž๐Ÿงต๐Ÿฌ๐ŸŒญ๐Ÿ‰๐Ÿ˜‘๐ŸฆŽ๐Ÿ›ธ๐Ÿ’๐Ÿ™๐Ÿฉ๐Ÿคœ๐Ÿ‘บ๐Ÿ•บ๐Ÿ›ซ๐Ÿ‘‰๐Ÿ‘–๐Ÿ˜ขโ›ฒ๐ŸŒญ๐Ÿ›ž๐Ÿฏ๐Ÿก๐Ÿ‘‚๐Ÿ’ฆ๐Ÿชณ๐Ÿก๐Ÿฎ๐Ÿ‘ฎ๐Ÿชณ๐Ÿจ๐ŸŒฝ๐Ÿ‘™๐Ÿ˜ฑ๐ŸŽจ๐Ÿคš๐ŸŽ…๐Ÿ˜๐Ÿซ๐Ÿ‘…๐Ÿ‘ฑ๐Ÿ˜ข๐Ÿซ๐Ÿ‘ƒ๐Ÿ’Š๐Ÿ”ช๐Ÿ“๐Ÿค’๐Ÿ‘ž๐Ÿ™๐Ÿ––๐Ÿ€๐Ÿ’ฉ๐Ÿšžโ›ต๐Ÿ”…๐ŸŽ€๐Ÿ™Ž๐Ÿคนโ™๐Ÿ›žโ˜•
80+
81+
```

0 commit comments

Comments
ย (0)