Last active
September 2, 2025 08:09
-
-
Save Eun/4b08b1d3ebc2c5d4d241bba4a71c2b58 to your computer and use it in GitHub Desktop.
example of using go templates with namespaces, e.g. strings.ToLower, strings.ToUpper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"fmt" | |
"net/url" | |
"os" | |
"path" | |
"path/filepath" | |
"strings" | |
"text/template" | |
) | |
type StringsContext struct{} | |
func (StringsContext) ToLower(s string) (string, error) { | |
return strings.ToLower(s), nil | |
} | |
func (StringsContext) ToUpper(s string) (string, error) { | |
return strings.ToUpper(s), nil | |
} | |
func (StringsContext) Trim(s, cutset string) (string, error) { | |
return strings.Trim(s, cutset), nil | |
} | |
func (StringsContext) TrimPrefix(s, prefix string) (string, error) { | |
return strings.TrimPrefix(s, prefix), nil | |
} | |
func (StringsContext) TrimSuffix(s, prefix string) (string, error) { | |
return strings.TrimSuffix(s, prefix), nil | |
} | |
type OSContext struct{} | |
func (OSContext) Hostname() (string, error) { | |
return os.Hostname() | |
} | |
func (OSContext) Getenv(key string) (string, error) { | |
return os.Getenv(key), nil | |
} | |
func (OSContext) Getuid() (int, error) { | |
return os.Getuid(), nil | |
} | |
func (OSContext) Getgid() (int, error) { | |
return os.Getgid(), nil | |
} | |
type PathContext struct{} | |
func (PathContext) Dir(s string) (string, error) { | |
return path.Dir(s), nil | |
} | |
func (PathContext) Base(s string) (string, error) { | |
return path.Base(s), nil | |
} | |
func (PathContext) Join(s ...string) (string, error) { | |
return path.Join(s...), nil | |
} | |
func (PathContext) Clean(s string) (string, error) { | |
return path.Clean(s), nil | |
} | |
func (PathContext) Ext(s string) (string, error) { | |
return path.Ext(s), nil | |
} | |
type FilePathContext struct{} | |
func (FilePathContext) Dir(s string) (string, error) { | |
return filepath.Dir(s), nil | |
} | |
func (FilePathContext) Base(s string) (string, error) { | |
return filepath.Base(s), nil | |
} | |
func (FilePathContext) Join(s ...string) (string, error) { | |
return filepath.Join(s...), nil | |
} | |
func (FilePathContext) Clean(s string) (string, error) { | |
return filepath.Clean(s), nil | |
} | |
func (FilePathContext) Ext(s string) (string, error) { | |
return filepath.Ext(s), nil | |
} | |
func (FilePathContext) Abs(s string) (string, error) { | |
return filepath.Abs(s) | |
} | |
func (FilePathContext) Rel(basepath, targetpath string) (string, error) { | |
return filepath.Rel(basepath, targetpath) | |
} | |
type URLContext struct{} | |
func (URLContext) JoinPath(base string, elem ...string) (string, error) { | |
return url.JoinPath(base, elem...) | |
} | |
func (URLContext) PathEscape(s string) (string, error) { | |
return url.PathEscape(s), nil | |
} | |
func (URLContext) PathUnescape(s string) (string, error) { | |
return url.PathUnescape(s) | |
} | |
func (URLContext) QueryEscape(s string) (string, error) { | |
return url.QueryEscape(s), nil | |
} | |
func (URLContext) QueryUnescape(s string) (string, error) { | |
return url.QueryUnescape(s) | |
} | |
func renderTemplate(tmpl string, data interface{}) (string, error) { | |
t := template.New("template") | |
t = t.Funcs(template.FuncMap{ | |
"filepath": func(args ...any) (any, error) { | |
return FilePathContext{}, nil | |
}, | |
"os": func(args ...any) (any, error) { | |
return OSContext{}, nil | |
}, | |
"path": func(args ...any) (any, error) { | |
return PathContext{}, nil | |
}, | |
"strings": func(args ...any) (any, error) { | |
return StringsContext{}, nil | |
}, | |
"url": func(args ...any) (any, error) { | |
return URLContext{}, nil | |
}, | |
}) | |
var err error | |
t, err = t.Parse(tmpl) | |
if err != nil { | |
return "", fmt.Errorf("failed to parse template: %w", err) | |
} | |
var buf bytes.Buffer | |
if err := t.Execute(&buf, data); err != nil { | |
return "", fmt.Errorf("failed to execute template: %w", err) | |
} | |
return buf.String(), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment