|
| 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