undefined kafka components for Go kafka

Viewed 3087

I was trying to install one of my go files. But I bumped into this error

C:\mygoproject>go install kafkapublisher.go

\#command-line-arguments
.\kafkapublisher.go:8:65: undefined: kafka.Message

.\kafkapublisher.go:10:19: undefined: kafka.NewProducer

.\kafkapublisher.go:10:38: undefined: kafka.ConfigMap

.\kafkapublisher.go:17:31: undefined: kafka.Event

.\kafkapublisher.go:19:26: undefined: kafka.Message

On my kafkapublisher.go file, I already imported the kafka dependency:

    import (
        "github.com/confluentinc/confluent-kafka-go/kafka"
        "log"
    )

even on my go.mod file

    module mymodule
    
    go 1.12
    
    require (
        github.com/aws/aws-lambda-go v1.15.0
        github.com/confluentinc/confluent-kafka-go v1.3.0
    )

I followed this documentation: https://docs.confluent.io/current/clients/go.html

screenshot

6 Answers

I was facing the same issues.

Kafka Go client is based on the C library. So setting flag CGO_ENABLED=1 will enable go to use C libraries for kafka client.

Hope it saves someone's time.

clearly dependencies are not being imported,

if you run the go build command, it will download the necessary dependencies and compile the code etc

try running go build ./...

The dependencies have not been downloaded.

You can either use a go get to download the package. Or Use

  1. go mod download
  2. go mod tidy
  3. go mod vendor - This will create a vendor folder with the required dependencies

When you build your image, use -tags musl (for alpine linux, @see github.com/confluentinc/confluent-kafka-go) and active CGO_ENABLED to activate lib in C because Kafka Go client is based on the C library librdkafka

In dockerfile :

FROM golang:1.16-alpine as builder

ARG GIT_TAG_NAME

RUN apk --no-cache update && \
apk --no-cache add git gcc libc-dev

# Kafka Go client is based on the C library librdkafka
ENV CGO_ENABLED 1
ENV GOFLAGS -mod=vendor
ENV GOOS=linux
ENV GOARCH=amd64

RUN export GO111MODULE=on

RUN go build -tags musl -ldflags "-s -w -X main.Version=$GIT_TAG_NAME" -o bin/main ./cmd/main

Note: -tags musl : if you want to use the built-in librdkafka -tags dynamic : if you want to use your own librdkafka.

CGO_ENABLED=1 go build -ldflags"$(shell ./build/scripts/go-build-ldflags.sh $(MODULE_ROOT)/ldflags)" -o bin/XXX/main.go

tips: test in mac & linux os ,as for reason,i think it depends on some C language library

Related