项目中使用到Go,两天看文档,一周出产品,第一次体验Go来开发,虽然写起代码很舒服,但是遇到xml转换是真的麻烦,没有php开发快啊,哈哈。不过Go是真的值得学习。先看下xml转结构体。
文件book.xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?> <bookstore name="新华书店"> <book category="CHILDREN"> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title>Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
解析xml的代码如下,代码即文档
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
)
// bookMap 定义xml中的book元素映射
type bookMap struct {
XMLName xml.Name `xml:"book"`
Title string `xml:"title"`
Author string `xml:"author"`
Year string `xml:"year"`
Price float64 `xml:"price"`
}
// xmlMap 定义xml总映射
type xmlMap struct {
XMLName xml.Name `xml:"bookstore"` // 第一个根节点 , 每一个结构体一个xml.name
BookStoreName string `xml:"name,attr"` // 第一个根节点的属性值是name
BookStruct []bookMap `xml:"book"` // 每一个book元素,遇到数组类型需要新增加一个结构体来映射,例如bookMap结构体
}
func main() {
// 定义xml文件位置
xmlFile := "book.xml"
// 读取xml文件内容
xmlContent, _ := ioutil.ReadFile(xmlFile)
// 定义接收xml映射的结构体
var xmlReceiveMap xmlMap
err := xml.Unmarshal(xmlContent, &xmlReceiveMap)
if err != nil {
fmt.Println(err)
return
}
// 输出书店名称
fmt.Println("书店名称" + xmlReceiveMap.BookStoreName)
// 循环输出每条记录
for index, book := range xmlReceiveMap.BookStruct {
fmt.Println(index, book)
}
}貌似xml有多层就得定义几层结构体,每层结构体都有1个XMLname,有点累
【一】.钩子文件的设置和创建(1).打开hooks目录,可以看到有一个post-commit.tmpl文件,这是一个模板文件。复制一份,重命名为post-commit,将其用户组设为www,并设置为可执行。chown www:www post-commitchmod +x post-commit(2...
Application 对象用于存储和访问来自任意页面的变量,类似 Session 对象。不同之处在于所有的用户分享一个 Application 对象,而 session 对象和用户的关系是一一对应的。很多的书籍中介绍的Application对象都喜欢以统计在线人数来介绍Application 对象...
1.远程仓库的协作模式开发者把自己最新的版本推到线上仓库,同时把线上仓库的最新代码,拉到自己本地即可2.注册git帐号国外: http://www.github.com国内: http://git.oschina.net2.在码云创建项目,不要初始化readmegit push https://gi...
日志查看:git log版本切换:方式1:git reset --hard HEAD^ 倒退一个版本git reset --hard HEAD^^ 倒退两个版本方式2:(版本号的形式,建议版本号码补充完...
1.关机Process.Start("shutdown", "-s -t 0"); 2. 注销 Proc...
private const string fantizi = "高久峰是個程序員"; private const string jiantizi = "高久峰是个程序员...