TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Serving static files with Go

3 点作者 shakedko超过 11 年前

1 comment

georgemcbay超过 11 年前
Nice writeup!<p>One other thing I&#x27;d mention to anyone considering serving static files in Go is this:<p>You might be tempted to cache the static files into a map for &quot;performance&quot;. After all memory is much faster than disk, right? Be careful with this, Go&#x27;s http FileServer uses sendto on platforms that support it and because it bypasses the userland&#x2F;kernel barrier and can thus do a zero-copy transmission, sendto (in combination with OS-level file caching) will almost certainly outperform your in-memory caching.<p>If you really want to optimize transmission, one way to do it (if you&#x27;re serving mostly data that isn&#x27;t already naturally compressed) is to gzip everything that is gzip-friendly either in a post-compile step or at runtime, and then serve an on-disk-cached (filename).gz version instead of (filename) with appropriate headers (and checking client headers to make sure they can accept gzip content), but still using the internal FileServer logic to make use of the sendto functionality. Go&#x27;s internal http stuff is very comprehensive but it is an API and not really a framework and it doesn&#x27;t try to do anything clever with gzipping of content.<p>As always, whenever &quot;optimizing&quot; make sure you measure extensively to make sure you&#x27;re actually helping and not making things worse.