8

I am trying to import and use cbrotli implementation from google as below:

import (
    "fmt"
    "io/ioutil"

    cbrotli "github.com/google/brotli/go/cbrotli"
)

But I am getting the below error while trying to run the program:

learn-go [master●●] % CGO_CFLAGS="-I /dev/projects/go/learn-go/src/brotli/c/include/brotli" go run cmd/compress/main.go
# github.com/google/brotli/go/cbrotli
src/github.com/google/brotli/go/cbrotli/reader.go:13:10: fatal error: 'brotli/decode.h' file not found
#include <brotli/decode.h>

I am not sure how to pass some C flags to make sure that I can use the brotli implementation

Aditya Singh
  • 13,438
  • 12
  • 35
  • 62

2 Answers2

6

Assuming you already have built brotli, if not, there is an installation instructions in their Github page:

$ mkdir out && cd out
$ ../configure-cmake
$ make
$ make test
$ make install

When building your Go app, you only need to pass -I ~<prefix>/include, where <prefix> is where you installed the header files for brotli. If you did not configure this prefix, it is usually in /usr/local.

After this, you can run using:

$ CGO_FLAGS='-I <prefix>/include' CGO_FLAGS='-L <prefix>/lib' LD_LIBRARY_PATH='<prefix>/lib' go run cmd/compress/main.go

Note: You don't need to add "brotli" at the end of your CGO_FLAGS

ssemilla
  • 3,542
  • 9
  • 27
0

A Go implementation is also available:

package main

import (
   "github.com/andybalholm/brotli"
   "net/http"
)

const in = "https://raw.githubusercontent.com" +
   "/google/brotli/master/tests/testdata/ukkonooa.compressed"

const out = "ukko nooa, ukko nooa oli kunnon mies, kun han meni saunaan, " +
   "pisti laukun naulaan, ukko nooa, ukko nooa oli kunnon mies."

func main() {
   g, e := http.Get(in)
   if e != nil {
      panic(e)
   }
   defer g.Body.Close()
   b := make([]byte, len(out))
   brotli.NewReader(g.Body).Read(b)
   println(string(b) == out)
}

https://pkg.go.dev/github.com/andybalholm/brotli

Steven Penny
  • 82,115
  • 47
  • 308
  • 348