From 655b6b211ae6df0186abd740f248939f7ddeaec1 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Mon, 31 Mar 2025 16:59:18 +0800 Subject: Add descriptive comments to most Go functions --- http_template_funcs.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'http_template_funcs.go') diff --git a/http_template_funcs.go b/http_template_funcs.go index f466f07..526841f 100644 --- a/http_template_funcs.go +++ b/http_template_funcs.go @@ -5,31 +5,35 @@ package main import ( "net/url" - "path" "strings" ) +// These are all trivial functions that are used in HTML templates. +// See resources.go. + +// firstLine returns the first line of a string. func firstLine(s string) string { before, _, _ := strings.Cut(s, "\n") return before } -func baseName(s string) string { - return path.Base(s) -} - +// pathEscape escapes the input as an URL path segment. func pathEscape(s string) string { return url.PathEscape(s) } +// queryEscape escapes the input as an URL query segment. func queryEscape(s string) string { return url.QueryEscape(s) } +// dereference dereferences a pointer. func dereference[T any](p *T) T { return *p } +// dereferenceOrZero dereferences a pointer. If the pointer is nil, the zero +// value of its associated type is returned instead. func dereferenceOrZero[T any](p *T) T { if p != nil { return *p @@ -38,6 +42,7 @@ func dereferenceOrZero[T any](p *T) T { return z } +// minus subtracts two numbers. func minus(a, b int) int { return a - b } -- cgit v1.2.3