Using Memcached with Go language.


The repository contains the basic code to start using Memcached with a Go client.

Installing Memcached

Multiple ways to install Memcached.

  1. On Ubuntu OS using package manager, apt install memcached. A drawback is the version installed might be old.

  2. Using the Memcached Docker Image.

  3. Installing from source, as shown below.

    1. Download the latest tar from here
    2. Follow the instructions there to install memecached.
    3. Memcached binary will now be available at /usr/local/memcahed/bin.
    4. Copy the memcached.service file from Scripts folder in the downloaded tar to /etc/systemd/system.
    5. Copy the memcached.sysconfig which is the environment file, to an appropriate location, eg :- /etc/sysconfig/ and possibly rename that file to memcached.
    6. Edit the memcached.service file to point to the correct environment file and binary.
    7. Issue sudo systemctl daemon-reload and start the service using sudo systemctl start memcached.
    8. Optionally issue sudo systemctl enable memcached to automatically start memcached during bootup.

Understanding this repository

  • Package cmd/basic/main.go has the basic code to save and retrive data from memcached server.

  • Package cmd/basic_serialize/main.go shows how to store and retrieve any type of data to memcached using gob package to serialize and deserialize the data.

    • One issue here is that gob fails when the byte array is decoded into an interface{} variable, and hence needs to be passed the variable of correct concrete type to be decoded into.
  • Package cmd/advanced_serialization/main.go has experimental code to work out how to decode the byte array received from memcached server into an interface{} variable using gob package. Haven’t been able to get a working solution.

  • Package cmd/with_msgpack/main.go uses msgpack which has no issues decoding a byte array into an interface{} variable.

  • Package cmd/with_msgpack_advanced/main.go uses encoding/json to unmarshall map values to concrete structs.

  • Package cmd/with_json/main.go uses encoding/json to both encode and decode values saved to memcached. Can be used with primitve types and concrete structs.


  1. Problem with encoding and decoding error types. Refer
    1. Will need to encode the actual error string and then when decoding will need to have a way to recognise the string is error and convert to error from there.


View Github