Home Backend Development Golang Message publishing and subscription model in Go language

Message publishing and subscription model in Go language

Jun 01, 2023 am 09:21 AM
go language news release Subscription model

With the continuous development and increasing demands of modern applications, more and more developers are beginning to turn their attention to the messaging mechanism. In this case, there is a type of message model that is concerned by many developers, and that is the message publication and subscription model. This model implements message passing in a simple and effective way and is widely used in distributed architectures. In this model, the Go language also has its own unique implementation method.

This article will introduce the message publishing and subscription model in Go language, including how to use Channels in Go language to implement and use the message publishing and subscription model, and how to implement a simple message in Go language queue.

1. Introduction to Go language Channels

Channel is a mechanism used to achieve concurrent communication in the Go language. Channels provide a way to transfer data between different goroutines (coroutines) and can be used to synchronize the execution between goroutines. Channels that pass data from one goroutine to another are thread-safe and can avoid race conditions.

In the Go language, use the make function to create a Channel. The syntax of the make function is as follows:

make(chan T)
Copy after login

Among them, T represents the element type in the Channel. For example, to create a Channel that delivers an integer type, you can use the following code:

ch := make(chan int)
Copy after login

2. Implementation of message publishing and subscription model in Go language

Implementation of message publishing and subscription model in Go language The method is very simple, just use Channel. The recommended message publishing and subscription model code examples in Go language are as follows:

package main

import (
    "fmt"
)

func main() {
    ch1 := make(chan string)
    ch2 := make(chan string)

    go func() {
        for {
            str := <-ch1
            ch2 <- "go " + str
        }
    }()

    for i := 0; i < 5; i++ {
        ch1 <- fmt.Sprintf("message %d", i)
    }

    for i := 0; i < 5; i++ {
        fmt.Println(<-ch2)
    }
}
Copy after login

The above code block uses two Channels: ch1 and ch2. We defined a goroutine that is responsible for reading messages from ch1, converting them to strings and prefixing them with "go", and then sending these new messages out through ch2. Then we generate some messages in the main goroutine and send them to ch1, and then we receive and print these new messages from ch2. This method is a common way to implement message publishing and subscription models in the Go language.

3. Implementing a simple message queue in Go language

It is also very simple to implement a simple message queue in Go language. You only need to use Channel and goroutine.

First, we define a queue type:

type Queue struct {
    items []string
    lock  sync.Mutex
    ch    chan bool
}
Copy after login

This queue type has three important member variables: items, lock and ch. Among them, items are used to store messages in the queue, lock is used to protect the write and read operations of the queue, and ch is used to notify the queue that new messages have arrived. Notification is implemented by sending a bool value to the Channel.

We also need to define a method for adding messages to the queue:

func (q *Queue) Add(item string) {
    q.lock.Lock()
    defer q.lock.Unlock()

    q.items = append(q.items, item)
    q.ch <- true
}
Copy after login

This method is thread-safe and can avoid the occurrence of race conditions. It first acquires the lock of the queue, then adds the message to the queue, and finally sends a bool value to the Channel.

We also need to define a method for getting messages for the queue:

func (q *Queue) Get() (string, bool) {
    q.lock.Lock()
    defer q.lock.Unlock()

    if len(q.items) == 0 {
        return "", false
    }

    item := q.items[0]
    q.items = q.items[1:]

    return item, true
}
Copy after login

This method is also thread-safe. It first obtains the lock of the queue, and then checks whether the queue is empty. If the queue is empty Returns false. Otherwise, it gets a message from the head of the queue, removes the head element, and returns the message and a true value.

The sample code for using this queue is as follows:

package main

import (
    "fmt"
    "time"
)

func main() {
    q := Queue{
        items: []string{},
        ch:    make(chan bool),
    }

    // 启动一个goroutine更新队列
    go func() {
        for {
            select {
            case <-q.ch:
                for {
                    item, ok := q.Get()
                    if !ok {
                        break
                    }
                    fmt.Println(item)
                }
            }
        }
    }()

    // 向队列中添加一些消息
    for i := 0; i < 5; i++ {
        q.Add(fmt.Sprintf("message %d", i))
        time.Sleep(time.Second)
    }
}
Copy after login

In the above code, we define a variable q of Queue type, then start a goroutine to update it, and finally add it to the queue Added some messages. The goroutine uses the select statement to get message notifications from the Channel, and gets all the messages in the queue and prints them.

Summary

The message publishing and subscription model in Go language is very simple and efficient, and has natural thread safety due to the use of Channels. This article introduces how to implement the message publishing and subscription model in Go language, and how to implement a simple message queue in Go language. After learning these contents, you can use them to implement various asynchronous processing tasks and improve the concurrency performance of the program.

The above is the detailed content of Message publishing and subscription model in Go language. 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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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

Java Tutorial
1664
14
PHP Tutorial
1267
29
C# Tutorial
1239
24
How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

What is the difference between `var` and `type` keyword definition structure in Go language? What is the difference between `var` and `type` keyword definition structure in Go language? Apr 02, 2025 pm 12:57 PM

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

Which libraries in Go are developed by large companies or provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

When using sql.Open, why does not report an error when DSN passes empty? When using sql.Open, why does not report an error when DSN passes empty? Apr 02, 2025 pm 12:54 PM

When using sql.Open, why doesn’t the DSN report an error? In Go language, sql.Open...

See all articles