Jed Colina
Jed Colina

Reputation: 241

undefined kafka components for Go kafka

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

Upvotes: 9

Views: 14661

Answers (7)

Jed Colina
Jed Colina

Reputation: 241

I already figured out this one. I installed Confluent's Kafka Go Client. Instructions are here: https://docs.confluent.io/current/clients/go.html#

The library is not supported on windows though, so I had to use virtual machine (Oracle VM Box) to build and run my code.

I also needed to compile and install librdkafka before installing the Confluent's GO Kafka Client: https://github.com/confluentinc/confluent-kafka-go/blob/master/README.md

Thanks.

Upvotes: 4

Ganesh Satpute
Ganesh Satpute

Reputation: 3951

If you're on Windows, follow these steps

  1. Install GCC. I installed from here https://jmeubank.github.io/tdm-gcc/download/
  2. Restart terminal
  3. Run SET CGO_ENABLED=1
  4. Run the failing command again.

Upvotes: 0

Shady Smaoui
Shady Smaoui

Reputation: 1285

  • Make sure to activate CGO_ENABLED -> CGO_ENABLED=1.
  • CGO_ENABLED is an environment variable in the Go programming language that controls whether the Go compiler includes support for calling C code.

Upvotes: 0

chandrakant kaski
chandrakant kaski

Reputation: 427

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.

Upvotes: 22

Adrien Parrochia
Adrien Parrochia

Reputation: 824

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.

Upvotes: 7

Anjula Paulus
Anjula Paulus

Reputation: 1

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

Upvotes: -1

Hemanth Kondapalli
Hemanth Kondapalli

Reputation: 1302

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

Upvotes: -1

Related Questions