golang twitz应用程序中的解析功能。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang twitz应用程序中的解析功能。相关的知识,希望对你有一定的参考价值。
var parseCmd = &cobra.Command{
Use: "parse",
Short: "This command will extract the Twitter Accounts form a text file.",
Long: `This command will extract the Twitter Accounts and clean up or disregard other characters
or text around the twitter accounts to create a simple, clean, Twitter Accounts only list.`,
Run: func(cmd *cobra.Command, args []string) {
completedTwittererList := buildTwitterList()
fmt.Println(completedTwittererList)
if viper.Get("fileExport") != nil {
exportParsedTwitterList(viper.GetString("fileExport"), viper.GetString("fileFormat"), completedTwittererList)
}
},
}
func exportParsedTwitterList(exportFilename string, exportFormat string, twittererList []string) {
if exportFormat == "txt" {
exportTxt(exportFilename, twittererList, exportFormat)
} else if exportFormat == "json" {
exportJson(exportFilename, twittererList, exportFormat)
} else if exportFormat == "xml" {
exportXml(exportFilename, twittererList, exportFormat)
} else if exportFormat == "csv" {
exportCsv(exportFilename, twittererList, exportFormat)
} else {
fmt.Println("Export type unsupported.")
}
}
func exportXml(exportFilename string, twittererList []string, exportFormat string) {
fmt.Printf("Starting xml export to %s.", exportFilename)
xmlContent, err := xml.Marshal(twittererList)
check(err)
header := xml.Header
collectedContent := header + string(xmlContent)
exportFile(collectedContent, exportFilename+"."+exportFormat)
}
func exportCsv(exportFilename string, twittererList []string, exportFormat string) {
fmt.Printf("Starting txt export to %s.", exportFilename)
collectedContent := rebuildForExport(twittererList, ",")
exportFile(collectedContent, exportFilename+"."+exportFormat)
}
func exportTxt(exportFilename string, twittererList []string, exportFormat string) {
fmt.Printf("Starting %s export to %s.", exportFormat, exportFilename)
collectedContent := rebuildForExport(twittererList, "\n")
exportFile(collectedContent, exportFilename+"."+exportFormat)
}
func exportJson(exportFilename string, twittererList []string, exportFormat string) {
fmt.Printf("Starting %s export to %s.", exportFormat, exportFilename)
collectedContent := collectContent(twittererList)
exportFile(string(collectedContent), exportFilename+"."+exportFormat)
}
func collectContent(twittererList []string) []byte {
collectedContent, err := json.Marshal(twittererList)
check(err)
return collectedContent
}
func rebuildForExport(twittererList []string, concat string) string {
var collectedContent string
for _, twitterAccount := range twittererList {
collectedContent = collectedContent + concat + twitterAccount
}
if concat == "," {
collectedContent = strings.TrimLeft(collectedContent, concat)
}
return collectedContent
}
func exportFile(collectedContent string, exportFile string) {
contentBytes := []byte(collectedContent)
err := ioutil.WriteFile(exportFile, contentBytes, 0644)
check(err)
}
json库在golang中的使用
参考技术A
golang对json序列化和反序列化的操作实在是难受,所以说用习惯了高级语言特性,再转到这些偏原生的写法上就会很难受。
不多BB,开始记录。
当写个小demo或者做个小工具,没有大规模使用场景,那使用哪个库都是一样的,因为性能的体现并不会很明显。但是如果是在实际项目中使用,且伴随着高并发,大容量等场景,我还是推荐使用 json-iterator 。
号称最快的go json解析器。跟官方的写法兼容,我目前基本都使用这个。
https://github.com/json-iterator/go
效率对比
ns 纳秒 op 操作
俩种方式,一种直接反序列化成 结构体数组,另一种反序列化为 slice,内容为map[string]interface
结构体数组
slice
http://www.zhouhuibo.club
以上是关于golang twitz应用程序中的解析功能。的主要内容,如果未能解决你的问题,请参考以下文章
golang 通过Twitter API为Twitz应用程序查找Twitter帐户列表的Findem功能。
golang twitz-配置,implementation.go
apache_conf twitz配置功能文件的主要代码。
golang中的flag模块小结
通过 Golang 中的模板解析自定义变量
如何在golang中解析请求中的json? [关闭]