Golang(9)File Management
7.1 XML
import (
“encoding/xml”
“fmt”
“io/ioutil”
“os"
)
xml.Unmarshal(data []byte, v inteface{}) error
xml.Marshal(v interface{}) ([]byte, error)
xml.MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)
7.2 JSON Handling
JSON(Javascript Object Notation)
Here is the sample JSON string
{“servers”:[{“serverName”:”Shanghai_VPN”, “serverIP”:”127.0.0.1”},{“serverName”:”Beijing_VPN”,”serverIP”:”127.0.0.2"}]}
Parse JSON
func Unmarshal(data []byte, v interface{}) error
Here is the example how to use this function.
package main
import (
"encoding/json"
"fmt"
)
type Server struct {
ServerName string
ServerIP string
}
type Serverslice struct {
Servers []Server
}
func main() {
var s Serverslice
str := `{"servers":[{"serverName":"Shanghai_VPN","serverIP":"127.0.0.1"},{"serverName":"Beijing_VPN","serverIP":"127.0.0.2"}]}`
json.Unmarshal([]byte(str), &s)
fmt.Println(s)
fmt.Println(s.Servers[0])
fmt.Println(s.Servers[0].ServerIP)
}
Remember to capital the first property name.
bool <——>JSON booleans
float64 <———>JSON numbers
string <————>JSON strings
nil <————> JSON null
We can use bitly simplejson to parse the unknown format JSON string.
https://github.com/bitly/go-simplejson
>go get github.com/bitly/go-simplejson
package main
import (
"fmt"
"github.com/bitly/go-simplejson"
)
func main() {
js, _ := simplejson.NewJson([]byte(`{
"test": {
"array": [1, 2, 3],
"int": 10,
"float": 5.150,
"bignum": 9223372036854775807,
"string": "simplejson",
"bool": true
}
}`))
arr, _ := js.Get("test").Get("array").Array()
i, _ := js.Get("test").Get("int").Int()
ms := js.Get("test").Get("string").MustString()
fmt.Println(arr)
fmt.Println(i)
fmt.Println(ms)
}
The console output is as follow:
>go run src/com/sillycat/easygoapp/main.go
[1 2 3] 10 simplejson
Generate JSON
func Marshal(v interface{}) ([]byte, error)
package main
import (
"encoding/json"
"fmt"
)
type Server struct {
ServerName string
ServerIP string
}
type Serverslice struct {
Servers []Server
}
func main() {
var s Serverslice
s.Servers = append(s.Servers, Server{ServerName: "Shanghai_VPN", ServerIP: "127.0.0.1"})
s.Servers = append(s.Servers, Server{ServerName: "Beijing_VPN", ServerIP: "127.0.0.2"})
b, err := json.Marshal(s)
if err != nil {
fmt.Println("json err:", err)
}
fmt.Println(string(b))
}
The console output should be as follow:
{"Servers":[{"ServerName":"Shanghai_VPN","ServerIP":"127.0.0.1"},{"ServerName":"Beijing_VPN","ServerIP":"127.0.0.2"}]}
7.3 Regex
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/07.3.md
7.4 Template
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/07.4.md
7.5 File Operation
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/07.5.md
7.6 String Operation
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/07.6.md
References:
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/07.0.md
- 浏览: 2552235 次
- 性别:
- 来自: 成都
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
相关推荐
* Golang build environment management * Compile and test using standard Golang tools and GOPATH * Custom GOPATH support system, IDE and project * Custom project build configuration * Golang ...
网络API 大猩猩Mux JWT令牌认证Golang Gorm(MSSQL)React组件还原源物质UI 轴距React路由器引导页引导箱先决条件转到1.9.1 node.js 8> VS COde或Jetbrains GoLand或原子编辑器数据库Open SQL Server Management ...
:file_folder: 自动化克隆的GitHub存储库的生命周期和组织 :scroll: 目录 :waving_hand: 入门 当您开始为不断增加的GitHub存储库做出贡献时,您很快就会意识到在计算机上...
9. **SFTP (SSH File Transfer Protocol)**: SFTP是SSH的一部分,用于安全地传输文件。在SSH2项目中,你可能需要使用SFTP客户端或库来处理文件操作。 10. **Authentication Methods**: SSH2支持多种身份验证...
koanf (发音为conf ,日语为Koan的剧本)是一个库,用于从Go应用程序中以不同格式读取来自不同来源的配置。 它是的更干净,更轻便的具有更好的抽象性和可扩展性以及更少的依赖性。 koanf内置支持从文件,命令行...
Efs2 您不希望可以像创建 Docker 映像一样轻松地配置服务器吗?...Efs2 文件Efs2file支持 Efs2 的配置; 很像Dockerfile ,这个文件使用一组简单的指令来配置我们的目标服务器。 # Install and Configure NGINX#
支架或名词(在伊利亚特(Iliad))大声喊叫的希腊先驱。 (小写)声音很大声或有力的人。 (小写)Stentor属的喇叭形纤毛原生动物。 stentor是用于从一组片段文件和模板生成更改日志或发行说明的CLI。...
mgmt :下一代配置管理!... import "datetime"$is_friday = datetime.weekday(datetime.now()) == "friday"file "/srv/files/" {state => $const.res.file.state.exists,mode => if $is_friday { # this up
:Libra: 所以你也来看我了?:face_with_monocle: 相识 漫长 了解 靠近 倾心 惊讶 窃喜 慌张 担忧 逃避 试探 ...:keycap_2::keycap_0::keycap_2::keycap_0::minus::keycap_3::minus::keycap_9::wavy_dash::keycap_3::minu
you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in ...
主机 该工具使您可以更好地控制主机文件的使用。 您可以具有多个配置文件,并根据需要将其打开/关闭。为什么? 通过手动编辑来处理hosts文件是一项繁琐的任务。 使用此工具,您可以使某些方面自动化,以使其变得更加...
(3) File extensions and packages:.......................................................................14 2.2 Go Environment variables...................................................................
图fig是一个很小的库,用于将应用程序的配置文件及其环境加载到Go结构中。 各个字段可以定义默认值,也可以根据需要标记。为什么是无花果? 在一个位置定义您的配置,验证和默认值也可以选择从环境加载仅3个外部依赖...
3.2.1. Golang LiteIDE ..........................................................................................32 3.2.2. GoClipse........................................................................