Ubuntu 18.04/20.04/22.04: libcrypto.so.1.0.0: cannot open shared object file: No such file or directory

这是因为高版本的ubuntu没有低版本的libcrypto.so1.0 libcrypto.so.1.1文件。

可以下载指定版本并安装,即可解决找不到文件的问题。

1. 直接下载deb包
wget https://debian.mirror.ac.za/debian/pool/main/o/openssl/libssl1.1_1.1.1w-0%2Bdeb11u1_amd64.deb
sudo dpkg -i libssl1.1_1.1.1o-1_amd64.deb

2. 在官网下载源码
#从官网下载
# wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz
 
#腾讯云提供的镜像wget https://mirrors.cloud.tencent.com/openssl/source/openssl-1.1.1g.tar.g

#安装
tar -xvf openssl-1.1.1g.tar.gz
cd openssl-1.1.1g
./config shared --openssldir=/usr/local/openssl --prefix=/usr/local/openssl
make && make install
nginx:no resolver defined to resolve xxx.xxx”

这是因为无法解析proxy_pass部分的域名,

在nginx.conf配置文件中的http{}部分添加一行resolver 8.8.8.8;

Go语言教程之边写边学:web应用:读取和写入JSON数据的Web应用程序

在此示例中,我们将通过用户界面表单读取-写入-更新JSON文件中的JSON数据,获取特定记录的数据并删除特定记录的数据。

net/http标准库包提供了多种创建HTTP服务器的方法,并附带了一个基本的路由器。此处使用的net/http包用于向服务器发出请求并响应请求。

 

项目目录结构:

 

 

示例代码:main.go

导入net/http包。包http提供HTTP客户端和服务器实现。在main函数中,使用HandleFunc方法创建路由 /。HandleFunc为给定模式注册处理程序函数。HandleFunc阻止我们在执行处理程序之前和之后使用中间件执行任务。ListenAndServe使用给定的地址和处理程序启动HTTP服务器。

package main

import (
	handlers "./handlers"
	"net/http"
)

func main() {
	http.HandleFunc("/addnewuser/", handlers.AddNewUserFunc)
	http.HandleFunc("/notsucceded", handlers.NotSucceded)

	http.HandleFunc("/deleted", handlers.DeletedFunc)
	http.HandleFunc("/deleteuser/deleted", handlers.DeleteUserFunc)
	http.HandleFunc("/deleteuser/", handlers.DeleteUserServe)
	http.HandleFunc("/deleteuser/notsuccededdelete", handlers.NotSuccededDelete)

	http.HandleFunc("/", handlers.IndexFunc)

	http.HandleFunc("/showuser/show", handlers.ShowUserFunc)
	http.HandleFunc("/showuser/", handlers.ShowUser)
	http.HandleFunc("/showuser/notsuccededshow/", handlers.NotSuccededShow)

	http.ListenAndServe(":8080", nil)
}

 

示例代码:list.json

[
 {
  "id": 1,
  "firstName": "Mariya",
  "lastName": "Ivanova",
  "balance": 300
 },
 {
  "id": 2,
  "firstName": "EKatina",
  "lastName": "Milevskaya",
  "balance": 5000
 },
 {
  "id": 3,
  "firstName": "Vasek",
  "lastName": "Zalupickiy",
  "balance": 2000
 }
]

 

示例代码:handlers\handlers.go

package handlers

import (
	model "../model"
	"encoding/json"
	"fmt"
	"html/template"
	"io/ioutil"
	"net/http"
	"os"
	"regexp"
	"strconv"
)

//function to check correct user adding input (regular expression and non-empty field input)
func checkFormValue(w http.ResponseWriter, r *http.Request, forms ...string) (res bool, errStr string) {
	for _, form := range forms {
		m, _ := regexp.MatchString("^[a-zA-Z]+$", r.FormValue(form))
		if r.FormValue(form) == "" {
			return false, "All forms must be completed"
		}
		if m == false {
			return false, "Use only english letters if firstname,lastname forms"
		}

	}
	return true, ""
}

func ShowUser(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, "templates/showUser.html")
}

func NotSuccededShow(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, "templates/notSuccededShow.html")

}

//handler to show user with id input
func ShowUserFunc(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		t, _ := template.ParseFiles("templates/showUserPage.html")
		t.Execute(w, nil)

	} else {

		id, err := strconv.Atoi(r.FormValue("id"))
		checkError(err)
		var alUsrs model.AllUsers
		file, err := os.OpenFile("list.json", os.O_RDONLY, 0666)
		checkError(err)
		b, err := ioutil.ReadAll(file)
		checkError(err)
		json.Unmarshal(b, &alUsrs.Users)

		var allID []int
		for _, usr := range alUsrs.Users {
			allID = append(allID, usr.Id)
		}
		for _, usr := range alUsrs.Users {
			if model.IsValueInSlice(allID, id) != true {
				http.Redirect(w, r, "/showuser/notsuccededshow/", 302)
				return
			}
			if usr.Id != id {
				continue
			} else {
				t, err := template.ParseFiles("templates/showUserPage.html")
				checkError(err)
				t.Execute(w, usr)
			}

		}
	}
}

//function to handle page with successful deletion
func DeletedFunc(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, "templates/deleted.html")
}

//serving file with error (add function:empty field input or uncorrect input)
func NotSucceded(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, "templates/notSucceded.html")
}

//function,which serve html file,when deleting was not succesful(id input is not correct)
func NotSuccededDelete(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, "templates/notSuccededDelete.html")
}

//function,which serve page with delete information input
func DeleteUserServe(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, "templates/deleteUser.html")

}

//function to delete user
func DeleteUserFunc(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		t, _ := template.ParseFiles("templates/deleteUser.html")
		t.Execute(w, nil)
	} else {
		r.ParseForm()
		id, err := strconv.Atoi(r.FormValue("id"))
		checkError(err)

		//open file with users
		file, err := os.OpenFile("list.json", os.O_RDWR|os.O_APPEND, 0666)
		defer file.Close()

		//read file and unmarshall json to []users
		b, err := ioutil.ReadAll(file)
		var alUsrs model.AllUsers
		err = json.Unmarshal(b, &alUsrs.Users)
		checkError(err)

		var allID []int
		for _, usr := range alUsrs.Users {
			allID = append(allID, usr.Id)
		}
		for i, usr := range alUsrs.Users {
			if model.IsValueInSlice(allID, id) != true {
				http.Redirect(w, r, "/deleteuser/notsuccededdelete", 302)
				return
			}
			if usr.Id != id {
				continue
			} else {
				alUsrs.Users = append(alUsrs.Users[:i], alUsrs.Users[i+1:]...)
			}

		}
		newUserBytes, err := json.MarshalIndent(&alUsrs.Users, "", " ")
		checkError(err)
		ioutil.WriteFile("list.json", newUserBytes, 0666)
		http.Redirect(w, r, "/deleted", 301)
	}
}

func checkError(err error) {
	if err != nil {
		fmt.Println(err)
	}
}

//function to add user
func AddNewUserFunc(w http.ResponseWriter, r *http.Request) {

	//creating new instance and checking method
	newUser := &model.User{}
	if r.Method == "GET" {
		t, _ := template.ParseFiles("templates/addNewUser.html")
		t.Execute(w, nil)

	} else {
		resBool, errStr := checkFormValue(w, r, "firstname", "lastname")
		if resBool == false {
			t, err := template.ParseFiles("templates/notSucceded.html")
			checkError(err)
			t.Execute(w, errStr)

			return
		}
		newUser.FirstName = r.FormValue("firstname")
		newUser.LastName = r.FormValue("lastname")
		var err error
		newUser.Balance, err = strconv.ParseFloat(r.FormValue("balance"), 64)
		checkError(err)

		//open file
		file, err := os.OpenFile("list.json", os.O_RDWR, 0644)
		checkError(err)
		defer file.Close()

		//read file and unmarshall json file to slice of users
		b, err := ioutil.ReadAll(file)
		var alUsrs model.AllUsers
		err = json.Unmarshal(b, &alUsrs.Users)
		checkError(err)
		max := 0

		//generation of id(last id at the json file+1)
		for _, usr := range alUsrs.Users {
			if usr.Id > max {
				max = usr.Id
			}
		}
		id := max + 1
		newUser.Id = id

		//appending newUser to slice of all Users and rewrite json file
		alUsrs.Users = append(alUsrs.Users, newUser)
		newUserBytes, err := json.MarshalIndent(&alUsrs.Users, "", " ")
		checkError(err)
		ioutil.WriteFile("list.json", newUserBytes, 0666)
		http.Redirect(w, r, "/", 301)

	}

}

//Index page handler
func IndexFunc(w http.ResponseWriter, r *http.Request) {
	au := model.ShowAllUsers()
	t, err := template.ParseFiles("templates/indexPage.html")
	checkError(err)
	t.Execute(w, au)
}

 

示例代码:model/model.go

package model

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
)

func checkError(err error) {
	if err != nil {
		fmt.Println(err)
	}
}

func IsValueInSlice(slice []int, value int) (result bool) {
	for _, n := range slice {
		if n == value {
			return true
		}

	}
	return false

}

type User struct {
	Id        int     `json:"id"`
	FirstName string  `json:"firstName"`
	LastName  string  `json:"lastName"`
	Balance   float64 `json:"balance"`
}

type AllUsers struct {
	Users []*User
}

func ShowAllUsers() (au *AllUsers) {
	file, err := os.OpenFile("list.json", os.O_RDWR|os.O_APPEND, 0666)
	checkError(err)
	b, err := ioutil.ReadAll(file)
	var alUsrs AllUsers
	json.Unmarshal(b, &alUsrs.Users)
	checkError(err)
	return &alUsrs
}

 

示例代码:templates\indexPage.html

<html>
<head>
	<title>User info</title>
</head>
<body>

{{range .Users}}
<div><h3> ID:{{ .Id }} </h3></div>
<div> First Name:{{ .FirstName }}</div>
<div> Last Name:{{ .LastName }} </div>
<div> Balance:{{ .Balance }}</div>
{{end}}

<h1>Options</h1>
<form action="/addnewuser/">
    <button type="submit">Add new user</button>
</form>
<form action="/deleteuser/">
    <button type="submit">Delete user</button>
</form>
<form action="/showuser/">
    <button type="submit">Show user</button>
</form>

</body>
</html>

 

示例代码:templates\addNewUser.html

<html>
<head>
	<title>Adding new user</title>
</head>
<body>
<h2>New user's data</h2>
        <form method="POST" action="useraded">
            <label>Enter firstname</label><br>
            <input type="text" name="firstname" /><br><br>
			<label>Enter lastname</label><br>
            <input type="text" name="lastname" /><br><br>
            <label>Enter balance</label><br>
            <input type="number" name="balance" /><br><br>
            <input type="submit" value="Submit" />
        </form>
</body>
</html>

 

示例代码:templates\deleteUser.html

<html>
<head>
	<title>Delete user</title>
</head>
<body>
<h2>Please,write an id of user you want to delete</h2>
        <form method="POST" action="deleted">
            <label>Enter id</label><br>
            <input type="text" name="id" /><br><br>
            <input type="submit" value="Submit" />
        </form>
</body>
</html>

 

示例代码:templates\deleted.html

<html>
<head>
	<title>User info</title>
</head>
<body>
	<div>Done</div><br><br>
	<form action="/">
		<button type="submit">Back to main</button>
	</form>
</body>
</html>

 

示例代码:templates\notSuccededDelete.html

<html>
<head>
	<title>User info</title>
</head>
<body>
<div>There is no user with such ID</div><br><br>
<form action="/">
    <button type="submit">Back to main</button>
</form>
</body>
</html>

 

示例代码:templates\notSuccededShow.html

<html>
<head>
	<title>User info</title>
</head>
<body>
<div>Error:Cant find User with such ID,try again</div><br><br>
<form action="/">
    <button type="submit">Back to main</button>
</form>
</body>
</html>

 

示例代码:templates\showUser.html

<html>
<head>
	<title>Show user</title>
</head>
<body>
<h2>Please,write an id of user you want to show</h2>
	<form method="POST" action="show">
		<label>Enter id</label><br>
		<input type="text" name="id" /><br><br>
		<input type="submit" value="Submit" />
	</form>
</body>
</html>

 

示例代码:templates\showUserPage.html

<html>
<head>
	<title>User info</title>
</head>
<body>
	<div><h3> ID:{{.Id}} </h3></div>
	<div> First Name:{{ .FirstName }}</div>
	<div> Last Name:{{ .LastName }} </div>
	<div> Balance:{{ .Balance }}</div>
</body>
</html>

 

示例代码:templates\notSucceded.html

<html>
<head>
	<title>User info</title>
</head>
<body>
<div>Error:{{.}}</div><br><br>
<form action="/">
    <button type="submit">Back to main</button>
</form>
</body>
</html>

 

运行应用程序:

go run main.go

 

在浏览器访问http://localhost:8080即可看到如下页面

Go语言教程之边写边学:常用的软件库:Golang中的动态JSON

Gabs是一个小型实用程序,用于处理Go中的动态或未知JSON结构。它不需要您知道有效负载的结构(例如创建结构),并允许通过提供字段的路径来访问字段。它是一个有用的包装器,用于访问encoding/json包提供的map[string]interface{} 对象的层次结构。

安装软件包:

go get github.com/Jeffail/gabs

 

示例代码:

package main

import (
	"fmt"

	"github.com/Jeffail/gabs"
)

func main() {
	data := []byte(`{
		"employees":{
		   "protected":false,
		   "address":{
			  "street":"22 Saint-Lazare",
			  "postalCode":"75003",
			  "city":"Paris",
			  "countryCode":"FRA",
			  "country":"France"
		   },
		   "employee":[
			  {
				 "id":1,
				 "first_name":"Jeanette",
				 "last_name":"Penddreth"
			  },
			  {
				 "id":2,
				 "firstName":"Giavani",
				 "lastName":"Frediani"
			  }
		   ]
		}
	 }`)

	jsonParsed, err := gabs.ParseJSON(data)
	if err != nil {
		panic(err)
	}

	// Search JSON
	fmt.Println("Get value of Protected:\t", jsonParsed.Path("employees.protected").Data())
	fmt.Println("Get value of Country:\t", jsonParsed.Search("employees", "address", "country").Data())
	fmt.Println("ID of first employee:\t", jsonParsed.Path("employees.employee.0.id").String())
	fmt.Println("Check Country Exists:\t", jsonParsed.Exists("employees", "address", "countryCode"))

	// Iterating address objects
	for key, child := range jsonParsed.Search("employees", "address").ChildrenMap() {
		fmt.Printf("Key=>%v, Value=>%v\n", key, child.Data().(string))
	}

	// Iterating employee array
	for _, child := range jsonParsed.Search("employees", "employee").Children() {
		fmt.Println(child.Data())
	}

	// Use index in your search
	for _, child := range jsonParsed.Search("employees", "employee", "0").Children() {
		fmt.Println(child.Data())
	}
}

输出:

Get value of Protected:  false
Get value of Country:    France
ID of first employee:    1
Check Country Exists:    true
Key=>street, Value=>22 Saint-Lazare
Key=>postalCode, Value=>75003
Key=>city, Value=>Paris
Key=>countryCode, Value=>FRA
Key=>country, Value=>France
map[id:1 first_name:Jeanette last_name:Penddreth]
map[id:2 firstName:Giavani lastName:Frediani]
Jeanette
Penddreth1
golang中struct在返回response时,不显示某个字段

用`json:"-"`方法可以隐藏对应字段。

type User struct {  
 ID       int    `json:"-"` // 隐藏字段,不包含在JSON中Name     string `json:"name"`  
 Age      int    `json:"age"`  
 HiddenField string `json:"-"` // 隐藏字段,不包含在JSON中  
}  
Go语言教程之边写边学:读写不同的文件类型

了解如何使用 bufio、encoding和 io 包读取和写入常见文件类型(文本、CSV、JSON 和 XML)中的数据。

 

读取 XML 文件

xml 包包括支持将数据从字节切片解码为值的函数Unmarshal()。该函数用于将 XML 格式文件中的值解码为Notes结构。

notes.xml文件是用os.ReadFile()函数读取的,返回一个字节片,然后使用xml.Unmarshal()函数将其解码为结构实例。结构实例成员值用于打印解码的数据。

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

package main
 
import (
	"encoding/xml"
	"fmt"
	"os"
)
 
type Notes struct {
	To      string `xml:"to"`
	From    string `xml:"from"`
	Heading string `xml:"heading"`
	Body    string `xml:"body"`
}
 
func main() {
	data, _ := os.ReadFile("notes.xml")
 
	note := &Notes{}
 
	_ = xml.Unmarshal([]byte(data), &note)
 
	fmt.Println(note.To)
	fmt.Println(note.From)
	fmt.Println(note.Heading)
	fmt.Println(note.Body)
}

输出

Tove
Jani
Reminder
Don't forget me this weekend!

 

写入 XML 文件

xml包有一个函数Marshal(),用于从结构序列化值并将其写入XML格式的文件。

notes结构由大写首字母定义,“xml”字段标记用于标识键。初始化结构值,然后使用xml.Marshal()函数进行序列化。接收序列化的XML格式的字节片,然后使用os.WriteFile将其写入文件。

package main
 
import (
	"encoding/xml"
	"os"
)
 
type notes struct {
	To      string `xml:"to"`
	From    string `xml:"from"`
	Heading string `xml:"heading"`
	Body    string `xml:"body"`
}
 
func main() {
	note := ¬es{To: "Nicky",
		From:    "Rock",
		Heading: "Meeting",
		Body:    "Meeting at 5pm!",
	}
 
	file, _ := xml.MarshalIndent(note, "", " ")
 
	_ = os.WriteFile("notes1.xml", file, 0644)
 
}

 

读取 JSON 文件

json包包括支持将数据从字节切片解码为值的函数Unmarshal()。解码后的值通常分配给结构字段,字段名称必须导出,并且应采用大写格式。

JSON文件test.json是使用os.ReadFile()读取的,它返回一个字节片,该片使用Unmarshal()函数把json解码为结构体实例。最后,使用for循环打印结构实例成员值,以证明JSON文件已解码。

package main
 
import (
	"encoding/json"
	"fmt"
	"os"
)
 
type CatlogNodes struct {
	CatlogNodes []Catlog `json:"catlog_nodes"`
}
 
type Catlog struct {
	Product_id string `json: "product_id"`
	Quantity   int    `json: "quantity"`
}
 
func main() {
	file, _ := os.ReadFile("test.json")
 
	data := CatlogNodes{}
 
	_ = json.Unmarshal([]byte(file), &data)
 
	for i := 0; i < len(data.CatlogNodes); i++ {
		fmt.Println("Product Id: ", data.CatlogNodes[i].Product_id)
		fmt.Println("Quantity: ", data.CatlogNodes[i].Quantity)
	}
 
}

 

写入 JSON 文件

json包有一个函数MarshalIndent(),用于从结构序列化值并将其写入JSON格式的文件。

 Salary结构是使用json字段定义的。初始化结构值,然后使用MarshalIndent()进行序列化。接收序列化的JSON格式的字节片,然后使用os.WriteFile()将其写入文件。

package main
 
import (
	"encoding/json"
	"io/ioutil"
)
 
type Salary struct {
        Basic, HRA, TA float64
    }
 
type Employee struct {
	FirstName, LastName, Email string
	Age                        int
	MonthlySalary              []Salary
}
 
func main() {
	data := Employee{
        FirstName: "Mark",
        LastName:  "Jones",
        Email:     "mark@gmail.com",
        Age:       25,
        MonthlySalary: []Salary{
            Salary{
                Basic: 15000.00,
                HRA:   5000.00,
                TA:    2000.00,
            },
            Salary{
                Basic: 16000.00,
                HRA:   5000.00,
                TA:    2100.00,
            },
            Salary{
                Basic: 17000.00,
                HRA:   5000.00,
                TA:    2200.00,
            },
        },
    }
 
	file, _ := json.MarshalIndent(data, "", " ")
 
	_ = ioutil.WriteFile("test.json", file, 0644)
}

输出

{
 "FirstName": "Mark",
 "LastName": "Jones",
 "Email": "mark@gmail.com",
 "Age": 25,
 "MonthlySalary": [
  {
   "Basic": 15000,
   "HRA": 5000,
   "TA": 2000
  },
  {
   "Basic": 16000,
   "HRA": 5000,
   "TA": 2100
  },
  {
   "Basic": 17000,
   "HRA": 5000,
   "TA": 2200
  }
 ]
}

 

读取文本文件

bufio包的Scanner通常用于按行或单词从文件中读取文本。以下源代码片段显示了从纯文本文件中逐行读取文本的过程。

os.Open()函数用于以只读模式打开特定的文本文件,这将返回os.File类型的指针。调用os.File.Close()来关闭os.File类型文件,并且有一个循环来循环访问和打印每个切片值。 执行后的程序在从文件中读取输出时逐行显示以下输出。

package main
 
import (
	"bufio"
	"fmt"
	"log"
	"os"
)
 
func main() {
	file, err := os.Open("test.txt")
 
	if err != nil {
		log.Fatalf("failed opening file: %s", err)
	}
 
	scanner := bufio.NewScanner(file)
	scanner.Split(bufio.ScanLines)
	var txtlines []string
 
	for scanner.Scan() {
		txtlines = append(txtlines, scanner.Text())
	}
 
	file.Close()
 
	for _, eachline := range txtlines {
		fmt.Println(eachline)
	}
}

输出

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nunc a mi dapibus, faucibus mauris eu, fermentum ligula.
Donec in mauris ut justo eleifend dapibus.
Donec eu erat sit amet velit auctor tempus id eget mauris.

 

写入文本文件

bufio包提供了一种高效的缓冲Writer,它将字节排队直到达到阈值,然后以最少的资源完成对文件的写入操作。以下源代码片段演示如何逐行将字符串切片写入纯文本文件。

sampledata表示为一个字符串切片,其中包含几行数据,这些数据将被写入文件中的新行。函数os.OpenFile()与标志组合一起使用,以创建只写文件(如果不存在),并在写入时附加到该文件。

package main
 
import (
	"bufio"
	"log"
	"os"
)
 
func main() {
	sampledata := []string{"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
		"Nunc a mi dapibus, faucibus mauris eu, fermentum ligula.",
		"Donec in mauris ut justo eleifend dapibus.",
		"Donec eu erat sit amet velit auctor tempus id eget mauris.",
	}
 
	file, err := os.OpenFile("test.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
 
	if err != nil {
		log.Fatalf("failed creating file: %s", err)
	}
 
	datawriter := bufio.NewWriter(file)
 
	for _, data := range sampledata {
		_, _ = datawriter.WriteString(data + "\n")
	}
 
	datawriter.Flush()
	file.Close()
}

 

读取 CSV 文件

csv包有一个函数NewReader(),该函数返回一个Reader对象来处理CSV数据。一个csv.Reader将其输入中的 \r\n 序列仅转换为 \n,其中还包括多行字段值。

test.csv只有几条记录,使用os.Open()函数以只读模式打开,该函数返回os.File的指针类型实例。csv.Reader.Read()方法用于将每个文件记录解码为预定义的结构CSVData,然后将它们存储在切片中,直到返回io.EOF。

package main
 
import (
	"bufio"
	"fmt"
	"log"
	"os"
)
 
func main() {
	file, err := os.Open("test.txt")
 
	if err != nil {
		log.Fatalf("failed opening file: %s", err)
	}
 
	scanner := bufio.NewScanner(file)
	scanner.Split(bufio.ScanLines)
	var txtlines []string
 
	for scanner.Scan() {
		txtlines = append(txtlines, scanner.Text())
	}
 
	file.Close()
 
	for _, eachline := range txtlines {
		fmt.Println(eachline)
	}
}

输出

Name -- City -- Job
John -- London -- CA
Micky -- Paris -- IT

 

写入CSV 文件

csv包有一个函数NewWriter(),该函数返回一个用于写入CSV数据的Writer对象。csv.Writer对象写入由换行符终止的csv记录,并使用逗号作为字段分隔符。以下源代码片段演示如何将数据写入CSV文件。

二维切片行包含示例csv记录。操作系统。os.Create()创建一个csv文件test.csv; 截断它的所有记录(如果已存在)并返回os.File对象。csvwriter.Write(row)方法以将每个字符串切片作为CSV记录写入文件。

package main
 
import (
	"encoding/csv"
	"log"
	"os"
)
 
func main() {
	rows := [][]string{
		{"Name", "City", "Language"},
		{"Pinky", "London", "Python"},
		{"Nicky", "Paris", "Golang"},
		{"Micky", "Tokyo", "Php"},
	}
 
	csvfile, err := os.Create("test.csv")
 
	if err != nil {
		log.Fatalf("failed creating file: %s", err)
	}
 
	csvwriter := csv.NewWriter(csvfile)
 
	for _, row := range rows {
		_ = csvwriter.Write(row)
	}
 
	csvwriter.Flush()
 
	csvfile.Close()
}
sogou和微信的关系

Sogou和微信都是腾讯的

2012年,腾讯投资搜狗,获得36.5%的股份。腾讯投资后,微信和搜狗开始全方位合作。直到现在,腾讯仍是搜狗的最大股东。在收购完成后,搜狗将成为腾讯全资子公司,搜狗原有的搜索、AI、输入法等业务和浏览器、阅读等产品将会和腾讯平台与内容事业群的同类产品并轨、团队合一,搜索和输入法等业务保持搜狗品牌运营。

搜狗搜索超过1/3的总流量来自于腾讯,其中一大部分来自微信。微信搜索长期使用搜狗搜索引擎,微信公众平台内容也独家开放给搜狗,搜狗为此上线了微信搜索专区。搜狗并入腾讯后,无疑将进一步加强微信的搜索实力,帮助微信更快实现其搜索布局。

搜狗问问平台和微信搜索强强合作,开启了口碑问答的新型方式,通过在搜狗问问上回答问题,微信搜索上也能够看得到,同样的在微信搜索上回答问题,也可以在搜狗问问上展示,这就实现了两个平台的联动合作,是问答推广的好阵地。

  • 当前日期:
  • 北京时间:
  • 时间戳:
  • 今年的第:18周
  • 我的 IP:18.220.224.115
农历
五行
冲煞
彭祖
方位
吉神
凶神
极简任务管理 help
+ 0 0 0
Task Idea Collect