go web的简单服务器
1)简单web服务器:
package main import (
"fmt"
"net/http"
) func sayHelloName(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "hello, world")
} func main() { http.HandleFunc("/", sayHelloName)
http.ListenAndServe(":9090", nil) }
首先go run hello.go之后,打开一个浏览器,输入http://127.0.0.1:9090,你就会在网页上看到web的打印了。
2)带有表单处理的web服务器:
package main
import (
"fmt"
"html/template"
"net/http"
)
func sayHelloName(w http.ResponseWriter, r* http.Request) {
fmt.Fprintf(w, "hello, world")
}
func login(w http.ResponseWriter, r* http.Request) {
if r.Method == "GET" {
t, _ := template.ParseFiles("login.html");
t.Execute(w, nil)
} else {
r.ParseForm()
fmt.Println("username:", r.Form["username"])
fmt.Println("password", r.Form["password"])
}
}
func main() {
http.HandleFunc("/", sayHelloName)
http.HandleFunc("/login", login)
http.ListenAndServe(":9090", nil)
}
上面给出的只是代码内容,你还需要一个login.html模板文件,
<html>
<head>
<title> </title>
</head> <body>
<form action="http://127.0.0.1:9090/login" method="post">
user: <input type="text" name ="username">
pass: <input type="password" name="password">
<input type="submit" value="login">
</form>
</body>
</html>
运行go代码之后,试着在浏览器下输入127.0.0.1:9090和127.0.0.1:9090/login
go web的简单服务器的更多相关文章
- Node安装及搭建简单服务器
注:本文安装系统为mac,windows及其他系统下载对应安装包 ,mac下载后的安装包为apk文件,windows为msi文件. 安装 1.在网上下载node安装包,官方网站2.双击下载文件,按步骤 ...
- Web界面的服务器监测工具(转载)
企业服务器对于企业业务持续性意义重大,系统管理员需要密切关注企业服务器以确保一切正常运行.当发现问题的时候,他们需要知道问题开始出现时的状况,因此调查可以重点放在问题出现的时候,这就意味着定期记录信息 ...
- Web Services简单介绍
Web Services简单介绍 Web Services入门 一.Web Services简介 1.什么是Web Services? Web Services 是应用程序组件 Web Service ...
- Java开发之使用websocket实现web客户端与服务器之间的实时通讯
使用websocket实现web客户端与服务器之间的实时通讯.以下是个简单的demo. 前端页面 <%@ page language="java" contentType=& ...
- Web Service 其他服务器检测不到查询测试按钮
1.Web Service 其他服务器检测不到查询测试按钮,config需要修改地方: <system.web> <webServices> <protocols> ...
- Web Service简单入门示例
Web Service简单入门示例 我们一般实现Web Service的方法有非常多种.当中我主要使用了CXF Apache插件和Axis 2两种. Web Service是应用服务商为了解决 ...
- Node.js初探之实现能向前台返回东西的简单服务器
nodejs nodejs文件就是一个简单的js文件. 在shell中运行 Step 1. 打开终端,进入这个js文件所在目#录 Step 2. 用 'node 文件名.js' 命令运行它即可. 用n ...
- 【netty】(2)---搭建一个简单服务器
netty(2)---搭建一个简单服务器 说明:本篇博客是基于学习慕课网有关视频教学.效果:当用户访问:localhost:8088 后 服务器返回 "hello netty"; ...
- 通过flask实现web页面简单的增删改查bootstrap美化版
通过flask实现web页面简单的增删改查bootstrap美化版 项目目录结构 [root@node1 python]# tree -L 2 . ├── animate.css ├── fileut ...
随机推荐
- 自定义及发布一个webservice服务
自定义及发布一个webservice服务 - 声明 某个业务服务为webservice服务 通过@webservice 注解来声明 - 发布webservice服务 ...
- LVS高可用集群的配置
网络结构: LVS DR工作原理 LVS集群从客户端上看可以将整个集群看成单个服务器对外提供服务,其IP是集群内部的VIP(虚拟IP).从内部看,转发服务器(DS)其实并没有启动应用层的服务对接口进行 ...
- (10)centos搭建web服务器 (Nginx+ django)
安装 python3 sudo yum install python34 安装uWSGI pip install uwsgi 安装 Nginx http://nginx.org/en/download ...
- Codeforces 746G(构造)
G. ...
- [Python Debug] SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
I Got a SettingWithCopyWarning when I ran the following code: tmp=date[date['date'].isnull().values= ...
- 《Microsoft SQL Server 2008 Internals》读书笔记--目录索引
http://blog.csdn.net/downmoon/article/details/5256548 https://sqlserverinternals.com/companion/
- mysql 性能容量评估
性能容量评估 分析上线业务场景 评估数据库服务器所需性能指标 预估可能成为瓶颈的服务器资源 帮助数据库性能调优 数据库服务器硬件性能指标项: 磁盘IO性能 内存容量 CPU 网络吞吐量 磁盘容 ...
- 不得不知Git远程操作详解
Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多优势,其中之一就是远程操作非常简便.本文详细介绍5个Git命令,它们的概念和用法,理解了这些内容,你就会完全掌握Gi ...
- Copy Records From One Data Block To Another Data Block In Oracle Forms
In this tutorial you will learn to copy the records from one data block to another data block on sam ...
- 【hibernate】hibernate不同版本的命名策略
===================================================hibernate 4命名策略如下================================ ...