我想在 Go 模板中制作类似 UI 组件 100% 可重用的东西,但我不知道是否可以做到。所以我正在尝试做下一步:
<code>{{define "components/menu-button"}} <a href="{{.link}}" class="text-white">{{.content}}</a> {{end}} </code>
这是我的组件,它需要一个 map
因为属性是小写的。
然后在我的主页中,我有一个小菜单,它在我的导航栏中使用了 components/menu-button
组件的 3 倍:
<code><div class="hidden gap-4 sm:flex"> {{template "components/menu-button" {"link": "/contact", "content": "Contact"}}} {{template "components/menu-button" {"link": "/docs", "content": "Docs"}}} {{template "components/menu-button" {"link": "/download", "content": "Download"}}} </div> </code>
但我不知道我是否可以以某种方式创建一个 map
就像我在示例中所做的那样,它就像 JSON 一样,但我尝试过。
顺便说一句,它给了我下一个错误:
unexpected "{" in template clause
正确答案
Go 的模板不支持这种语法。
你可以做的是声明一个自定义函数,例如
func MakeMap(kvs ...any) map[any]any { m := make(map[any]any) for i := 0; i < len(kvs)-1; i+=2 { m[kvs[i]] = kvs[i+1] } return m }
然后您可以使用 使该函数可用于模板(*模板).Funcs
,例如
t.Funcs(template.FuncMap{"M":MakeMap})
然后,在模板内,您可以使用密钥 M
调用该函数。
{{template "components/menu-button" (M "link" "/contact" "content" "Contact")}}
https://www.php.cn/link/0740bb92e583cd2b88ec7c59f985cb41
以上是如何在Go模板中传入'途中创建”的地图的详细内容。更多信息请关注PHP中文网其他相关文章!

Go语言使用"encoding/binary"包进行二进制编码与解码。1)该包提供binary.Write和binary.Read函数,用于数据的写入和读取。2)需要注意选择正确的字节序(如BigEndian或LittleEndian)。3)数据对齐和错误处理也是关键,确保数据的正确性和性能。

1)usebybytes.joinforconcatenatinges,2)bytes.bufferforincrementalWriter,3)bytes.indexorbytes.indexorbytes.indexbyteforsearching bytes.bytes.readereforrednerncretinging.isnchunk.ss.ind.inc.softes.4)

theencoding/binarypackageingoiseforporptimizingBinaryBinaryOperationsDuetoitssupportforendiannessessandefficityDatahandling.toenhancePerformance:1)usebinary.nativeendiandiandiandiandiandiandiandian nessideendian toavoid avoidByteByteswapping.2)

Go的bytes包主要用于高效处理字节切片。1)使用bytes.Buffer可以高效进行字符串拼接,避免不必要的内存分配。2)bytes.Equal函数用于快速比较字节切片。3)bytes.Index、bytes.Split和bytes.ReplaceAll函数可用于搜索和操作字节切片,但需注意性能问题。

字节包提供了多种功能来高效处理字节切片。1)使用bytes.Contains检查字节序列。2)用bytes.Split分割字节切片。3)通过bytes.Replace替换字节序列。4)用bytes.Join连接多个字节切片。5)利用bytes.Buffer构建数据。6)结合bytes.Map进行错误处理和数据验证。

Go的encoding/binary包是处理二进制数据的工具。1)它支持小端和大端字节序,可用于网络协议和文件格式。2)可以通过Read和Write函数处理复杂结构的编码和解码。3)使用时需注意字节序和数据类型的一致性,尤其在不同系统间传输数据时。该包适合高效处理二进制数据,但需谨慎管理字节切片和长度。

“字节”包装封装becapeitoffersefficerSoperationsOnbyteslices,cocialforbinarydatahandling,textPrococessing,andnetworkCommunications.byteslesalemutable,允许forforforforforformance-enhangingin-enhangingin-placemodifications,makaythisspackage

go'sstringspackageIncludeSessentialFunctionsLikeContains,trimspace,split,andReplaceAll.1)contunsefefitedseffitedseffiticefliceCheckSforSubStrings.2)trimspaceRemovesWhitespaceToeensuredity.3)splitparseSseSsess structertextrentextrentedTextlikeCsv.4)replastextlikecsv.4)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

WebStorm Mac版
好用的JavaScript开发工具