Help us shape the future of Go! Take the 2026 Go Developer Survey

memcached

package module
v0.44.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container struct {
	testcontainers.Container
}

Container represents the Memcached container type used in the module

func Run

Run creates an instance of the Memcached container type

Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/testcontainers/testcontainers-go"
	"github.com/testcontainers/testcontainers-go/modules/memcached"
)

func main() {
	// runMemcachedContainer {
	ctx := context.Background()

	memcachedContainer, err := memcached.Run(ctx, "memcached:1.6-alpine")
	defer func() {
		if err := testcontainers.TerminateContainer(memcachedContainer); err != nil {
			log.Printf("failed to terminate container: %s", err)
		}
	}()
	if err != nil {
		log.Printf("failed to start container: %s", err)
		return
	}
	// }

	state, err := memcachedContainer.State(ctx)
	if err != nil {
		log.Printf("failed to get container state: %s", err)
		return
	}

	fmt.Println(state.Running)

}
Output:
true
Example (Connect)
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/bradfitz/gomemcache/memcache"

	"github.com/testcontainers/testcontainers-go"
	"github.com/testcontainers/testcontainers-go/modules/memcached"
)

func main() {
	ctx := context.Background()

	memcachedContainer, err := memcached.Run(ctx, "memcached:1.6-alpine")
	defer func() {
		if err := testcontainers.TerminateContainer(memcachedContainer); err != nil {
			log.Printf("failed to terminate container: %s", err)
		}
	}()
	if err != nil {
		log.Printf("failed to start container: %s", err)
		return
	}

	// hostPort {
	hostPort, err := memcachedContainer.HostPort(ctx)
	if err != nil {
		log.Printf("failed to get host and port: %s", err)
		return
	}
	// }

	mc := memcache.New(hostPort)

	err = mc.Set(&memcache.Item{Key: "foo", Value: []byte("my value")})
	if err != nil {
		log.Printf("failed to set item: %s", err)
		return
	}

	it, err := mc.Get("foo")
	if err != nil {
		log.Printf("failed to get item: %s", err)
		return
	}

	fmt.Println(string(it.Value))

}
Output:
my value

func (*Container) HostPort

func (c *Container) HostPort(ctx context.Context) (string, error)

HostPort returns the host and port of the Memcached container

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL