⚠️ Malware / Trojaner / VirenAnthropic Says Russian Hackers Used Claude AI to Automate Malware Evasion(11.09.2026 um 10:47 Uhr)
🕵️ SicherheitslückenCheck Point Patches Critical VPN Vulnerabilities(11.09.2026 um 13:10 Uhr)
⚠️ Malware / Trojaner / VirenUkrainian Conti Ransomware Developer Sentenced to 4 Years in US Prison(11.09.2026 um 13:29 Uhr)
🕵️ SicherheitslückenGitLab Vulnerability Exploited One Day After Disclosure(11.09.2026 um 18:11 Uhr)
🔧 AI Nachrichten OpenAI Targets Work of Wall Street Junior Bankers(10.09.2026 um 21:02 Uhr)
🔧 AI Nachrichten Altman Considers Slowing Down AI Development(11.09.2026 um 20:00 Uhr)
⚠️ Malware / Trojaner / VirenMessengerdienste: Ermittler lesen Telegram und Whatsapp ohne Trojaner mit(03.09.2026 um 09:57 Uhr)
⚠️ Malware / Trojaner / VirenAnthropic Says Russian Hackers Used Claude AI to Automate Malware Evasion(11.09.2026 um 10:47 Uhr)
🕵️ SicherheitslückenCheck Point Patches Critical VPN Vulnerabilities(11.09.2026 um 13:10 Uhr)
⚠️ Malware / Trojaner / VirenUkrainian Conti Ransomware Developer Sentenced to 4 Years in US Prison(11.09.2026 um 13:29 Uhr)
🕵️ SicherheitslückenGitLab Vulnerability Exploited One Day After Disclosure(11.09.2026 um 18:11 Uhr)
🔧 AI Nachrichten OpenAI Targets Work of Wall Street Junior Bankers(10.09.2026 um 21:02 Uhr)
🔧 AI Nachrichten Altman Considers Slowing Down AI Development(11.09.2026 um 20:00 Uhr)
⚠️ Malware / Trojaner / VirenMessengerdienste: Ermittler lesen Telegram und Whatsapp ohne Trojaner mit(03.09.2026 um 09:57 Uhr)

🔧 Programmierung 🕛 vor 2 Jahren 5 Min Lesezeit
0

Go program pattern 05 : Decorations

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Go is a statically typed compiled language designed to be concise and efficient. While Go is not a purely object-oriented language, we can still use design patterns to improve code readability and maintainability. Today, I will introduce a common design pattern: the Decorator pattern.




This article is first published in the medium MPP plan. If you are a medium user, please follow me in medium. Thank you very much.







What is the Decorator Pattern?



The Decorator pattern is a design pattern that allows us to dynamically add behavior to an object at runtime without altering its implementation. This is achieved by creating a wrapper object or decorator that contains the original object and provides an enhanced interface to add new behavior.



In Go, we can use functions as decorators because Go supports higher-order functions, which means functions can be passed as parameters and returned as values.






An Example



To better understand the Decorator pattern, let's see how we can implement it in Go through an example.



First, we define a function type Foo and a decorator type FooDecorator:




CODE
type Foo func(string) string

type FooDecorator func(Foo) Foo






Then, we can create a decorator that takes a function of type Foo and returns a new function of type Foo which adds some behavior before and after calling the original function:




CODE
func WithLog(decorated Foo) Foo {
return func(s string) string {
fmt.Println("Before calling the decorated function")
result := decorated(s)
fmt.Println("After calling the decorated function")
return result
}
}






Now, we can create a Foo function and enhance it using the decorator:




CODE
func main() {
foo := func(s string) string {
fmt.Println("Foo function called")
return s
}

foo = WithLog(foo)

foo("Hello, world!")
}






In this example, we create a Foo function and use the WithLog decorator to enhance it. When we call the enhanced function, it first prints a message, then calls the original Foo function, and finally prints another message.



This is the Decorator pattern in Go. By using decorators, we can dynamically add new behavior without modifying the original function.






An HTTP-related Example



Next, let's look at an example related to handling HTTP requests. First, we'll start with a simple HTTP server code:




CODE
package main

import (
"fmt"
"log"
"net/http"
"strings"
)

func WithServerHeader(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("--->WithServerHeader()")
w.Header().Set("Server", "HelloServer v0.0.1")
h(w, r)
}
}

func hello(w http.ResponseWriter, r *http.Request) {
log.Printf("Received Request %s from %s\n", r.URL.Path, r.RemoteAddr)
fmt.Fprintf(w, "Hello, World! "+r.URL.Path)
}

func main() {
http.HandleFunc("/v1/hello", WithServerHeader(hello))
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}






In this code, we use the Decorator pattern. The WithServerHeader() function acts as a decorator that takes an http.HandlerFunc and returns a modified version. This example is relatively simple, as we only add a response header using WithServerHeader(). However, we can create many more functions like this, such as writing authentication cookies, checking authentication cookies, and logging.




CODE
package main

import (
"fmt"
"log"
"net/http"
"strings"
)

func WithServerHeader(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("--->WithServerHeader()")
w.Header().Set("Server", "HelloServer v0.0.1")
h(w, r)
}
}

func WithAuthCookie(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("--->WithAuthCookie()")
cookie := &http.Cookie{Name: "Auth", Value: "Pass", Path: "/"}
http.SetCookie(w, cookie)
h(w, r)
}
}

func WithBasicAuth(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("--->WithBasicAuth()")
cookie, err := r.Cookie("Auth")
if err != nil || cookie.Value != "Pass" {
w.WriteHeader(http.StatusForbidden)
return
}
h(w, r)
}
}

func WithDebugLog(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("--->WithDebugLog")
r.ParseForm()
log.Println(r.Form)
log.Println("path", r.URL.Path)
log.Println("scheme", r.URL.Scheme)
log.Println(r.Form["url_long"])
for k, v := range r.Form {
log.Println("key:", k)
log.Println("val:", strings.Join(v, ""))
}
h(w, r)
}
}

func hello(w http.ResponseWriter, r *http.Request) {
log.Printf("Received Request %s from %s\n", r.URL.Path, r.RemoteAddr)
fmt.Fprintf(w, "Hello, World! "+r.URL.Path)
}

func main() {
http.HandleFunc("/v1/hello", WithServerHeader(WithAuthCookie(hello)))
http.HandleFunc("/v2/hello", WithServerHeader(WithBasicAuth(hello)))
http.HandleFunc("/v3/hello", WithServerHeader(WithBasicAuth(WithDebugLog(hello))))
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}









Pipeline of Multiple Decorators



When using multiple decorators, the code can become less visually appealing as we need to nest functions layer by layer. However, we can refactor the code to make it cleaner. To do this, we first write a utility function that iterates through and calls each decorator:




CODE
type HttpHandlerDecorator func(http.HandlerFunc) http.HandlerFunc

func Handler(h http.HandlerFunc, decors ...HttpHandlerDecorator) http.HandlerFunc {
for i := range decors {
d := decors[len(decors)-1-i] // iterate in reverse
h = d(h)
}
return h
}






Then, we can use it like this:




CODE
http.HandleFunc("/v4/hello", Handler(hello,
WithServerHeader, WithBasicAuth, WithDebugLog))









Conclusion



In this article, I demonstrated the Decorator pattern using two examples. However, since Go does not support annotations as a syntactic sugar, using decorators can be a bit cumbersome. Nevertheless, the concept is still important, and we can apply this way of thinking to write higher-quality code in our daily development.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Text Watermarking in Python: Catch Whoever Copies Your Writing
1 Quelle
Why Most Multi-Agent Systems Fail Even When Evaluation Passes
1 Quelle
A Beginner’s Guide to World Models
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Go program pattern 05 : Decorations

Thematisch verwandte Begriffe: program, pattern, Decorations · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...