Reputation: 241
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
Upvotes: 9
Views: 14661
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
Reputation: 3951
If you're on Windows, follow these steps
SET CGO_ENABLED=1
Upvotes: 0
Reputation: 1285
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
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
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
Reputation: 1
The dependencies have not been downloaded.
You can either use a go get to download the package. Or Use
Upvotes: -1
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