Go Client

Go microservices—small binaries, fast startup, goroutine concurrency—fit cloud platforms that still must post transactions to IBM MQ queues beside COBOL and Java on the mainframe. The ibmmq Go package exposes MQI through Go types: you fill ibmmq.MQCNO and ibmmq.MQCD, call ibmmq.Connx, open queues, put and get byte slices, and interpret ibmmq.MQIMError for reason codes. Go does not remove the need for SVRCONN definitions, listener ports, or AUTHREC grants; it only makes the caller code idiomatic. Teams shipping distroless containers forget to copy libmqm and wonder why the binary runs locally but fails in Kubernetes. This tutorial covers module setup, connection strings, put and get with error handling, goroutines and connection safety, CCDT environment variables, TLS configuration pointers, graceful shutdown on context cancel, and operational alignment when Go workers scale beside Node and Java consumers on the same queue.

Module and Build Setup

Initialize go.mod and require the IBM mq-golang module version matching your queue manager support. Build tags and CGO may apply because the package links native code—read IBM readme for MQ_INSTALLATION_PATH pointing at client installation. Multi-stage Dockerfile: stage one installs client and builds binary; stage two copies client libs and binary into runtime image. go test in CI needs same client path as production or tests skip integration cases.

Go ibmmq call sequence
StepFunctionNote
1ConnxQueue manager connection handle
2OpenQueue object handle
3Put / GetMD PMO GMO structures
4CloseRelease object
5DiscDisconnect QM

Connect Example

go
1
2
3
4
5
6
7
8
9
10
11
12
13
import "github.com/ibm-messaging/mq-golang/v2/ibmmq" cd := ibmmq.NewMQCD() cd.ChannelName = "ORDERS.API" cd.ConnectionName = "mq-prod.corp(1414)" cno := ibmmq.NewMQCNO() cno.ClientConn = cd qMgr, err := ibmmq.Connx("QM_PROD", cno) if err != nil { mqret := err.(*ibmmq.MQIMError) // log mqret.MQRC } defer ibmmq.Disc(qMgr)

NewMQCD and NewMQCNO zero structures correctly for your header version. defer Disc on shutdown path—context cancellation in main should call cleanup. MQIMError exposes MQRC for metrics and alerting dashboards.

Put and Get

Open with MQOO_OUTPUT or INPUT_AS_Q_DEF. Put passes message descriptor and buffer []byte. Get loops with wait option until ctx.Done. Large messages allocate buffers sized to queue MAXMSGL—handle truncation errors. Persistent delivery sets persistence field in MQMD equivalent Go struct per package API.

Goroutines and Concurrency

IBM documents thread safety for connection handles—typical pattern is one connection per worker goroutine or synchronize access per handle. Do not share one Hconn across uncontrolled goroutines without reading current guidance. Worker pools with channel of jobs each holding dedicated connection reduce race risk. MAXINST on SVRCONN still caps total connections from all Go pods times replicas.

CCDT TLS and Kubernetes

  • os.Setenv MQCHLLIB before Connx in init.
  • Mount TLS certs as secrets; set SSL fields on CNO per docs.
  • Liveness probe may open short connection or check sidecar.
  • PreStop hook Disc before pod kill during deploy.

Compared to Java and Node

Go binary is smaller than JVM but still needs native MQ client unlike pure HTTP clients. Choose Go when team standardizes on Kubernetes operators written in Go; choose Java when existing JMS estate dominates. Same server CHSTATUS and AUTHREC diagnostics apply to all languages.

Troubleshooting

  1. Build fails CGO—client headers missing.
  2. Runtime cannot load libmqm—PATH in container.
  3. MQRC 2035—server grant for MCAUSER.
  4. MQRC 2538—network CONNAME.
  5. Goroutine leak—ensure Disc on all paths.

Explainer: Small Binary, Same Phone Line

Go compiles a small robot that still must use the same MQ telephone line (SVRCONN) as bigger Java robots.

Explain Like I'm Five: Go Client

Your Go program sends letters through the IBM MQ post office using a Go-friendly mailbox helper that speaks the post office language inside.

Practice Exercises

Exercise 1

Write Connx with MQCD and log MQRC on failure.

Exercise 2

Design worker pool with one MQ connection per worker and context shutdown.

Exercise 3

List Dockerfile COPY instructions for client libs on Linux amd64.

Frequently Asked Questions

Frequently Asked Questions

Test Your Knowledge

Test Your Knowledge

1. Go MQ module is:

  • ibmmq / mq-golang
  • net/http only
  • gin only
  • cobra only

2. Go client needs at runtime:

  • MQ native client libraries
  • Only Go binary
  • JRE
  • Node

3. Connx uses:

  • MQCNO MQCD structures
  • SQL DSN
  • FTP URL
  • JCL

4. Many goroutines should:

  • Respect MQ handle threading rules
  • Share one Conn without care
  • Skip disconnect
  • Ignore mqrc
Published
Read time20 min
AuthorMainframeMaster
Verified: IBM MQ 9.3 documentation