Table of Contents
Define the Observer Interface
Define the Subject (Publisher)
Implement Concrete Observers
Usage Example
Key Points
Home Backend Development Golang How do you implement an observer pattern in Golang?

How do you implement an observer pattern in Golang?

Aug 14, 2025 pm 12:04 PM
golang Observer pattern

In Go, observer mode can be implemented through interfaces and channels, the Observer interface can be defined, the Observer interface includes the Update method, the Subject structure maintains the observer list and message channel, add observers through Attach, Notify sends messages, listen goroutine asynchronous broadcast updates, specific observers such as EmailService and LogService implement the Update method to handle notifications, the main program registers the observer and triggers events, and realizes a loosely coupled event notification mechanism, which is suitable for event-driven systems, logging and message notifications, etc.

How do you implement an observer pattern in Golang?

In Go, you can implement the observer pattern using interfaces and channels to enable loose coupling between a subject (also called publisher) and its observers (subscribers). The idea is that when the subject's state changes, all registered observers are notified automatically.

Here's how you can implement it cleanly in Go:


Define the Observer Interface

Start by defining an interface that all observers must satisfy:

 type Observer interface {
    Update(message string)
}

Each observer will implement the Update method to react to changes.


Define the Subject (Publisher)

The subject maintains a list of observers and provides methods to add, remove, and notify them:

 type Subject struct {
    observers []Observer
    messages chan string
}

We use a channel ( messages ) to push updates asynchronously.

Initialize the subject:

 func NewSubject() *Subject {
    subject := &Subject{
        observers: make([]Observer, 0),
        messages: make(chan string, 10), // buffered channel
    }
    go subject.listen()
    Return subject
}

The listen method runs in a goroutine and broadcasts messages to all observers:

 func (s *Subject) listen() {
    for msg := range s.messages {
        for _, obs := range s.observers {
            obs.Update(msg)
        }
    }
}

Add methods to manage observers and trigger updates:

 func (s *Subject) Attach(obs Observer) {
    s.observers = append(s.observers, obs)
}

func (s *Subject) Notify(msg string) {
    s.messages <- msg
}

Note: We don't expose a Detach method here for simplicity, but you can add it by filtering the slice.


Implement Concrete Observers

Here's a simple observer that prints the message:

 type EmailService struct{}

func (e *EmailService) Update(message string) {
    fmt.Printf("Email sent with message: %s\n", message)
}

type LogService struct{}

func (l *LogService) Update(message string) {
    fmt.Printf("Log recorded: %s\n", message)
}

Usage Example

 func main() {
    subject := NewSubject()

    emailService := &EmailService{}
    logService := &LogService{}

    subject.Attach(emailService)
    subject.Attach(logService)

    subject.Notify("User registered!")
    subject.Notify("Payment processed.")

    // Give time for messages to be processed
    time.Sleep(time.Millisecond * 100)
}

Output:

 Email sent with message: User registered!
Log recorded: User registered!
Email sent with message: Payment processed.
Log recorded: Payment processed.

Key Points

  • Channels make the notification asynchronous and safe for concurrent use.
  • Interfaces allows flexibility — any type that implements Update can be an observer.
  • You can extend this with typed messages, error handling, or unsubscription logic.
  • For production use, consider using sync.RWMutex if observers are added/removed at runtime.

This pattern works well for event-driven systems, logging, notifications, or any scenario where components need to react to state changes without tight coupling. It's simple, idiomatic Go, and avoids complex dependencies.

The above is the detailed content of How do you implement an observer pattern in Golang?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1596
276
Understanding the Performance Differences Between Golang and Python for Web APIs Understanding the Performance Differences Between Golang and Python for Web APIs Jul 03, 2025 am 02:40 AM

Golangofferssuperiorperformance,nativeconcurrencyviagoroutines,andefficientresourceusage,makingitidealforhigh-traffic,low-latencyAPIs;2.Python,whileslowerduetointerpretationandtheGIL,provideseasierdevelopment,arichecosystem,andisbettersuitedforI/O-bo

Golang pointer to interface explanation Golang pointer to interface explanation Jul 21, 2025 am 03:14 AM

An interface is not a pointer type, it contains two pointers: dynamic type and value. 1. The interface variable stores the type descriptor and data pointer of the specific type; 2. When assigning the pointer to the interface, it stores a copy of the pointer, and the interface itself is not a pointer type; 3. Whether the interface is nil requires the type and value to be judged at the same time; 4. When the method receiver is a pointer, only the pointer type can realize the interface; 5. In actual development, pay attention to the difference between the value copy and pointer transfer of the interface. Understanding these can avoid runtime errors and improve code security.

How to run shell command in golang How to run shell command in golang Jul 07, 2025 am 12:47 AM

Execute Shell commands in Go language can be implemented through the standard library os/exec. The basic method is to use exec.Command() to create a command object and call Output() to get the result; 1. Create a command object when executing a simple command and call Output() to get the output; 2. When real-time output is required, use StdoutPipe and StderrPipe to execute and print while executing; 3. For complex commands containing pipelines or redirects, they can be handed over to /bin/sh-c for analysis and processing; 4. In terms of security, avoid splicing user input, and it is recommended to pass a parameter list; 5. The control command background operation can be achieved by combining Start() and Wait();

Describe the Observer design pattern and its implementation in PHP. Describe the Observer design pattern and its implementation in PHP. Aug 15, 2025 pm 01:54 PM

TheObserverdesignpatternenablesautomaticnotificationofdependentobjectswhenasubject'sstatechanges.1)Itdefinesaone-to-manydependencybetweenobjects;2)Thesubjectmaintainsalistofobserversandnotifiesthemviaacommoninterface;3)Observersimplementanupdatemetho

C   observer pattern implementation C observer pattern implementation Jul 05, 2025 am 01:27 AM

The observer pattern is a behavioral design pattern used to establish a one-to-many dependency between objects. It maintains a set of Observers through Subject and automatically notifies all observers when their state changes. The specific implementation steps are as follows: 1. Define the Observer interface, including the update() method; 2. Implement the Subject class, use the container to manage the observer list, and provide attach, detach and notify methods; 3. Create the ConcreteObserver class to implement specific update logic. Notes include: using smart pointers to avoid memory leaks; detaching destroyed observers in a timely manner; considering thread-safe operations; and controlling notification order based on demand.

What are the alternatives to standard library logging in Golang? What are the alternatives to standard library logging in Golang? Aug 05, 2025 pm 08:36 PM

FornewGo1.21 projects,useslogforofficialstructuredloggingsupport;2.Forhigh-performanceproductionservices,chooseZaporZerologduetotheirspeedandlowallocations;3.ForeaseofuseandrichintegrationslikeSlackorSentryhooks,Logrusisidealdespitelowerperformance;4

What are the best practices for API versioning in a Golang service? What are the best practices for API versioning in a Golang service? Aug 04, 2025 pm 04:50 PM

UseURLpathversioninglike/api/v1forclear,routable,anddeveloper-friendlyversioning.2.Applysemanticversioningwithmajorversions(v1,v2)only,avoidingmicro-versionslikev1.1topreventroutingcomplexity.3.OptionallysupportcontentnegotiationviaAcceptheadersifalr

How to work with NoSQL databases like MongoDB in Golang How to work with NoSQL databases like MongoDB in Golang Aug 03, 2025 pm 03:55 PM

Install MongoDBGo driver and use mongo.Connect() to establish a connection to ensure the connection is successful through Ping; 2. Define a Go structure with bson tag to map MongoDB documents, optionally use primitive.ObjectID as the ID type; 3. Use InsertOne to insert a single document, FindOne query a single document and handle mongo.ErrNoDocuments errors, UpdateOne updates the document, DeleteOne deletes the document, Find cooperates with cursor.All to get multiple documents; 4. Always use a context with timeout to avoid request hangs, and reuse Mon

See all articles