Installation

Lindera Go is a Go module and the library of Lindera C, which the module calls through cgo. go get fetches the module; the library is installed apart, from the archive of a release (The library), and the go command is told where it is (Building a program).

Prerequisites

  • Go 1.26 or later
  • A C compiler, which cgo runs: gcc or clang on Linux and macOS, a MinGW-w64 gcc on Windows
  • pkg-config, on Linux and macOS
  • The library of Lindera C, of the version of the module

The library

From the release after 7.3.0, each release has an archive of the library for each system, lindera-c-<target>-<version>.zip (see Pre-built archives for the targets). The module of a version goes with the archive of that version.

# Example: the library for Linux on x86_64
curl -LO https://github.com/lindera/lindera/releases/download/v<version>/lindera-c-x86_64-unknown-linux-gnu-<version>.zip
unzip lindera-c-x86_64-unknown-linux-gnu-<version>.zip

The directory can be anywhere. There is no archive for Linux with musl.

From the sources of Lindera, cargo-c installs the same files, and a script writes the pkg-config file of the static library, which an archive has:

cargo cinstall --release -p lindera-c --target-dir target/capi --prefix "$PWD/dist" --libdir lib
scripts/write_lindera_static_pc.sh "$PWD/dist"

The module

From the release after 7.3.0, the module has a version for each release of Lindera:

go get github.com/lindera/lindera/lindera-go/v7@v<version>

A program imports it by its path, and uses the package lindera:

import (
	"fmt"
	"os"
	"strings"

	"github.com/lindera/lindera/lindera-go/v7"
)

The path ends with the major version of Lindera: a program that moves to the next major version changes the path of its imports.

Building a program

Linux and macOS

pkg-config finds the library, through the directory of the archive:

export PKG_CONFIG_PATH="$PWD/lindera-c-x86_64-unknown-linux-gnu-<version>/lib/pkgconfig"
go build

The program links the static library (lindera-static.pc of the archive), so it runs by itself: the library does not have to be where the program runs.

With the build tag lindera_dynamic the program links the shared library instead, and needs it where the loader looks for it when it starts. On Linux:

go build -tags lindera_dynamic
LD_LIBRARY_PATH="$PWD/lindera-c-x86_64-unknown-linux-gnu-<version>/lib" ./tokenize /path/to/lindera-ipadic

On macOS the library of an archive names itself @rpath/liblindera.dylib, so the program gets a run path to its directory:

CGO_LDFLAGS="-Wl,-rpath,$PWD/lindera-c-aarch64-apple-darwin-<version>/lib" go build -tags lindera_dynamic

Windows

cgo uses a MinGW-w64 gcc, which links the DLL of the archive (its static library is for the MSVC toolchain). CGO_CFLAGS and CGO_LDFLAGS name the directories of the header and of the DLL, and the DLL is found through PATH when the program runs. In Git Bash:

export CGO_CFLAGS="-I$PWD/lindera-c-x86_64-pc-windows-msvc-<version>/include"
export CGO_LDFLAGS="-L$PWD/lindera-c-x86_64-pc-windows-msvc-<version>/bin"
go build
PATH="$PWD/lindera-c-x86_64-pc-windows-msvc-<version>/bin:$PATH" ./tokenize.exe /path/to/lindera-ipadic

Other settings

With the build tag lindera_flags the package names no library, and CGO_CFLAGS and CGO_LDFLAGS say everything: for a system without pkg-config, or a library that is somewhere else. On Linux, for the static library:

export CGO_CFLAGS="-I$PWD/dist/include"
export CGO_LDFLAGS="$PWD/dist/lib/liblindera.a -lgcc_s -lutil -lrt -lpthread -lm -ldl -lc"
go build -tags lindera_flags

The libraries after liblindera.a are those of Libs.private of lindera.pc.

After the library changes

The go command does not rebuild a package when the library it links has changed: after another version of the library is installed, or after the settings above change, clean the build cache.

go clean -cache

Obtaining Dictionaries

The library of an archive embeds no dictionary. A program loads one from a directory.

Pre-built dictionaries are available on the GitHub Releases page. Download and extract the dictionary archive; the archive holds one directory, lindera-ipadic for IPADIC:

# Example: download and extract the IPADIC dictionary
curl -LO https://github.com/lindera/lindera/releases/download/<version>/lindera-ipadic-<version>.zip
unzip lindera-ipadic-<version>.zip -d /path/to

The dictionary is then /path/to/lindera-ipadic, which is what a program passes to SetDictionary or to LoadDictionary.

A library built from the sources with an embed-* feature holds the dictionary, and a program names it as embedded://<name>, for example embedded://ipadic (see Feature Flags).

Development

The Makefile has the targets of the binding:

make format-lindera-go   # gofmt
make lint-lindera-go     # fail when gofmt would change a file
make test-lindera-go     # go vet and the tests, against the library cargo-c installs

make test-lindera-go builds the library of Lindera C with the features of its tests and installs it under target/capi, then runs go vet, the tests under the race detector, the tests with the checks of cgo, the tests with the shared library, and the example of the Quick Start from a module of its own (scripts/test_lindera_go.sh). It needs cargo-c (make setup-lindera-c-tools).

A test compares the functions and the constants of the C API (lindera-c/abi/symbols.txt, lindera.h) with what the package uses: one the C API gains is used by the package, or listed in lindera-go/capi_test.go with the reason it is not.