How to import and use CGO Brotli implementation from google?
Asked Answered
G

3

10

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

Griddle answered 11/11, 2017 at 0:49 Comment(0)
L
7

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

Laicize answered 1/11, 2018 at 4:32 Comment(0)
E
1

If installing on macos, you can use brew just fine (see formulae for version).

brew install brotli 

# Copy the symlinks from brew prefix dir into /usr/local
sudo cp -r $(brew --prefix)/include/brotli /usr/local/include/.
sudo cp -r $(brew --prefix)/lib/libbrotli* /usr/local/lib/.  

# Compiler will check /usr/local by default.
CGO_ENABLED=1 go build ...
Elisabeth answered 26/1, 2023 at 15:50 Comment(0)
C
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

Combustible answered 24/5, 2021 at 22:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.