提供最简单的方法创建web应用程序在Go语言中

网友投稿 568 2022-10-27

提供最简单的方法创建web应用程序在Go语言中

提供最简单的方法创建web应用程序在Go语言中

web.go

web.go is the simplest way to write web applications in the Go programming language. It's ideal for writing simple, performant backend web services.

Overview

web.go should be familiar to people who've developed websites with higher-level web frameworks like sinatra or web.py. It is designed to be a lightweight web framework that doesn't impose any scaffolding on the user. Some features include:

Routing to url handlers based on regular expressionsSecure cookiesSupport for fastcgi and scgiWeb applications are compiled to native code. This means very fast execution and page render speedEfficiently serving static files

Installation

Make sure you have the a working Go environment. See the install instructions. web.go targets the Go release branch.

To install web.go, simply run:

go get github.com/hoisie/web

To compile it from source:

git clone git://github.com/hoisie/web.gitcd web && go build

Example

package main import ( "github.com/hoisie/web") func hello(val string) string { return "hello " + val } func main() { web.Get("/(.*)", hello) web.Run("0.0.0.0:9999")}

To run the application, put the code in a file called hello.go and run:

go run hello.go

You can point your browser to http://localhost:9999/world .

Getting parameters

Route handlers may contain a pointer to web.Context as their first parameter. This variable serves many purposes -- it contains information about the request, and it provides methods to control the http connection. For instance, to iterate over the web parameters, either from the URL of a GET request, or the form data of a POST request, you can access ctx.Params, which is a map[string]string:

package mainimport ( "github.com/hoisie/web") func hello(ctx *web.Context, val string) { for k,v := range ctx.Params { println(k, v) }} func main() { web.Get("/(.*)", hello) web.Run("0.0.0.0:9999")}

In this example, if you visit http://localhost:9999/?a=1&b=2, you'll see the following printed out in the terminal:

a 1b 2

Documentation

API docs are hosted at https://hoisie.github.io/web/

If you use web.go, I'd greatly appreciate a quick message about what you're building with it. This will help me get a sense of usage patterns, and helps me focus development efforts on features that people will actually use.

About

web.go was written by Michael Hoisie

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring在多线程下@Resource注入为null的问题
下一篇:#yyds干货盘点# 面试必刷TOP101:反转链表
相关文章

 发表评论

暂时没有评论,来抢沙发吧~