CINXE.COM
lu4p's blog
<!doctype html><html lang=en dir=auto> <head><meta charset=utf-8> <meta http-equiv=x-ua-compatible content="IE=edge"> <meta name=viewport content="width=device-width,initial-scale=1,shrink-to-fit=no"> <meta name=robots content="index, follow"> <title>lu4p's blog</title> <meta name=description content="I write post about technical topics, mainly golang."> <meta name=author content="lu4p"> <link rel=canonical href=https://blog.lu4p.xyz/> <link crossorigin=anonymous href=/assets/css/stylesheet.min.c88963fe2d79462000fd0fb1b3737783c32855d340583e4523343f8735c787f0.css integrity="sha256-yIlj/i15RiAA/Q+xs3N3g8MoVdNAWD5FIzQ/hzXHh/A=" rel="preload stylesheet" as=style> <link rel=icon href=https://blog.lu4p.xyz/favicon.ico> <link rel=icon type=image/png sizes=16x16 href=https://blog.lu4p.xyz/favicon-16x16.png> <link rel=icon type=image/png sizes=32x32 href=https://blog.lu4p.xyz/favicon-32x32.png> <link rel=apple-touch-icon href=https://blog.lu4p.xyz/apple-touch-icon.png> <link rel=mask-icon href=https://blog.lu4p.xyz/safari-pinned-tab.svg> <meta name=theme-color content="#2e2e33"> <meta name=msapplication-TileColor content="#2e2e33"> <meta name=generator content="Hugo 0.88.1"> <link rel=alternate type=application/rss+xml href=https://blog.lu4p.xyz/index.xml> <link rel=alternate type=application/json href=https://blog.lu4p.xyz/index.json> <noscript> <style>#theme-toggle,.top-link{display:none}</style> <style>@media(prefers-color-scheme:dark){:root{--theme:rgb(29, 30, 32);--entry:rgb(46, 46, 51);--primary:rgb(218, 218, 219);--secondary:rgb(155, 156, 157);--tertiary:rgb(65, 66, 68);--content:rgb(196, 196, 197);--hljs-bg:rgb(46, 46, 51);--code-bg:rgb(55, 56, 62);--border:rgb(51, 51, 51)}.list{background:var(--theme)}.list:not(.dark)::-webkit-scrollbar-track{background:0 0}.list:not(.dark)::-webkit-scrollbar-thumb{border-color:var(--theme)}}</style> </noscript><meta property="og:title" content="lu4p's blog"> <meta property="og:description" content="I write post about technical topics, mainly golang."> <meta property="og:type" content="website"> <meta property="og:url" content="https://blog.lu4p.xyz/"><meta property="og:site_name" content="lu4p"> <meta name=twitter:card content="summary"> <meta name=twitter:title content="lu4p's blog"> <meta name=twitter:description content="I write post about technical topics, mainly golang."> <script type=application/ld+json>{"@context":"https://schema.org","@type":"Organization","name":"lu4p's blog","url":"https://blog.lu4p.xyz","description":"I write post about technical topics, mainly golang.","thumbnailUrl":"https://blog.lu4p.xyz/favicon.ico","sameAs":["https://github.com/lu4p","https://twitter.com/lu4p3","https://keybase.io/lu4p","https://gophers.slack.com/team/U0164H6DG2K"]}</script> </head> <body class=list id=top> <script>localStorage.getItem("pref-theme")==="dark"?document.body.classList.add('dark'):localStorage.getItem("pref-theme")==="light"?document.body.classList.remove('dark'):window.matchMedia('(prefers-color-scheme: dark)').matches&&document.body.classList.add('dark')</script> <header class=header> <nav class=nav> <div class=logo> <a href=https://blog.lu4p.xyz accesskey=h title="lu4p (Alt + H)">lu4p</a> <span class=logo-switches> <button id=theme-toggle accesskey=t title="(Alt + T)"><svg id="moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentcolor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1111.21 3 7 7 0 0021 12.79z"/></svg><svg id="sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentcolor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="5"/><line x1="12" y1="1" x2="12" y2="3"/><line x1="12" y1="21" x2="12" y2="23"/><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/><line x1="1" y1="12" x2="3" y2="12"/><line x1="21" y1="12" x2="23" y2="12"/><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/></svg> </button> </span> </div> <ul id=menu> <li> <a href=https://blog.lu4p.xyz/search/ title="Search (Alt + /)" accesskey=/> <span>Search</span> </a> </li> </ul> </nav> </header> <main class=main> <article class="first-entry home-info"> <header class=entry-header> <h1>Hi there 👋</h1> </header> <section class=entry-content> <p>Welcome to my blog</p> </section> <footer class=entry-footer> <div class=social-icons> <a href=https://github.com/lu4p target=_blank rel="noopener noreferrer me" title=Github><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentcolor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 19c-5 1.5-5-2.5-7-3m14 6v-3.87a3.37 3.37.0 00-.94-2.61c3.14-.35 6.44-1.54 6.44-7A5.44 5.44.0 0020 4.77 5.07 5.07.0 0019.91 1S18.73.65 16 2.48a13.38 13.38.0 00-7 0C6.27.65 5.09 1 5.09 1A5.07 5.07.0 005 4.77 5.44 5.44.0 003.5 8.55c0 5.42 3.3 6.61 6.44 7A3.37 3.37.0 009 18.13V22"/></svg> </a> <a href=https://twitter.com/lu4p3 target=_blank rel="noopener noreferrer me" title=Twitter><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentcolor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M23 3a10.9 10.9.0 01-3.14 1.53 4.48 4.48.0 00-7.86 3v1A10.66 10.66.0 013 4s-4 9 5 13a11.64 11.64.0 01-7 2c9 5 20 0 20-11.5a4.5 4.5.0 00-.08-.83A7.72 7.72.0 0023 3z"/></svg> </a> <a href=https://keybase.io/lu4p target=_blank rel="noopener noreferrer me" title=Keybase><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 71 76.3" fill="currentcolor" stroke="none"><path d="M6.68 73.99c-.6-1.3-1.4-3.1-1.8-4l-.6-1.7-2 2.2-2 2.2-.2-4.2c-.3-6 .2-12.2 1.2-16.6 2.3-9.8 9.5-18.7 18.8-23.4l2.1-1-.5-1.5c-.3-.8-.6-2.5-.7-3.6l-.2-2.1-2.1-.2c-3.2-.3-4.9-1.2-6-3.5-.6-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.1 2.9 3 0 1.1-.1 1.4-.8 2s-1 .8-2 .8c-.9.0-1.5-.2-1.9-.5z"/></svg> </a> <a href=https://gophers.slack.com/team/U0164H6DG2K target=_blank rel="noopener noreferrer me" title=Slack><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentcolor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22.08 9C19.81 1.41 16.54-.35 9 1.92S-.35 7.46 1.92 15 7.46 24.35 15 22.08 24.35 16.54 22.08 9z"/><line x1="12.57" y1="5.99" x2="16.15" y2="16.39"/><line x1="7.85" y1="7.61" x2="11.43" y2="18.01"/><line x1="16.39" y1="7.85" x2="5.99" y2="11.43"/><line x1="18.01" y1="12.57" x2="7.61" y2="16.15"/></svg> </a> </div> </footer> </article> <article class=post-entry> <header class=entry-header> <h2>Understanding HTML templates in Go (golang) </h2> </header> <section class=entry-content> <p>I use Echo in this post, but you could also use plain net/http, or any of the other awesome web frameworks for Go, see a list here. The Go standard library provides a html/template package, for dynamically rendering HTML, it is built on top of text/template Go templates are normal HTML files, with “Actions” (data evaluations or control structures), these “Actions” are delimited via {{and}}. A template is executed via applying a data structure to it, the data structure is referenced as a dot ....</p> </section> <footer class=entry-footer><span title="2020-12-30 03:28:14 +0100 +0100">December 30, 2020</span> · 4 min · lu4p</footer> <a class=entry-link aria-label="post link to Understanding HTML templates in Go (golang)" href=https://blog.lu4p.xyz/posts/golang-template-turbo/></a> </article> <article class=post-entry> <header class=entry-header> <h2>Inner workings of allocating slices with Go (golang) </h2> </header> <section class=entry-content> <p>I wasn’t sure what the performance impact of preallocating a slice with make vs. just letting the slice grow via append is, so I investigated a little. Arrays In order to understand what a slice is one needs to first understand how arrays work in go. An array’s type definition specifies a length and an element type. For example, the type [3]int represents an array of three integers. An array’s size is fixed, its length is part of the type, so [2]int and [3]int are incompatible, e....</p> </section> <footer class=entry-footer><span title="2020-12-02 22:23:20 +0100 +0100">December 2, 2020</span> · 6 min · lu4p</footer> <a class=entry-link aria-label="post link to Inner workings of allocating slices with Go (golang)" href=https://blog.lu4p.xyz/posts/slices/></a> </article> </main> <footer class=footer> <span>© 2022 <a href=https://blog.lu4p.xyz>lu4p's blog</a></span> <span> Powered by <a href=https://gohugo.io/ rel="noopener noreferrer" target=_blank>Hugo</a> & <a href=https://git.io/hugopapermod rel=noopener target=_blank>PaperMod</a> </span> </footer> <a href=#top aria-label="go to top" title="Go to Top (Alt + G)" class=top-link id=top-link accesskey=g><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 12 6" fill="currentcolor"><path d="M12 6H0l6-6z"/></svg> </a> <script>let menu=document.getElementById('menu');menu&&(menu.scrollLeft=localStorage.getItem("menu-scroll-position"),menu.onscroll=function(){localStorage.setItem("menu-scroll-position",menu.scrollLeft)}),document.querySelectorAll('a[href^="#"]').forEach(a=>{a.addEventListener("click",function(b){b.preventDefault();var a=this.getAttribute("href").substr(1);window.matchMedia('(prefers-reduced-motion: reduce)').matches?document.querySelector(`[id='${decodeURIComponent(a)}']`).scrollIntoView():document.querySelector(`[id='${decodeURIComponent(a)}']`).scrollIntoView({behavior:"smooth"}),a==="top"?history.replaceState(null,null," "):history.pushState(null,null,`#${a}`)})})</script> <script>var mybutton=document.getElementById("top-link");window.onscroll=function(){document.body.scrollTop>800||document.documentElement.scrollTop>800?(mybutton.style.visibility="visible",mybutton.style.opacity="1"):(mybutton.style.visibility="hidden",mybutton.style.opacity="0")}</script> <script>document.getElementById("theme-toggle").addEventListener("click",()=>{document.body.className.includes("dark")?(document.body.classList.remove('dark'),localStorage.setItem("pref-theme",'light')):(document.body.classList.add('dark'),localStorage.setItem("pref-theme",'dark'))})</script> <script async defer src=https://scripts.simpleanalyticscdn.com/latest.js></script> <noscript><img src=https://queue.simpleanalyticscdn.com/noscript.gif alt></noscript> </body> </html>