package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/gin-gonic/gin"
)
// Phone asd
type Phone interface {
call()
}
// MiPhone asd
type MiPhone struct {
}
func (miPhone MiPhone) call() {
fmt.Println("i im xiaomi phone")
}
func main() {
fmt.Println("start---->")
copyFile()
fmt.Println("end---->")
}
func copyFile() {
read, err := ioutil.ReadFile("name.txt")
if err != nil {
fmt.Println("错误", err)
}
fmt.Println(string(read))
ioutil.WriteFile("copy.txt", read, 0644)
}
func face() {
var ph Phone
ph = new(MiPhone)
ph.call()
}
// map 类型
func runMap() {
var object = make(map[string]string)
object["首都"] = "北京"
object["城市"] = "上海"
object["京城"] = "南京"
object["国度"] = "中国"
for key, val := range object {
fmt.Printf("key is %s val; is %s", key, val)
fmt.Println("")
}
delete(object, "首都")
delete(object, "国度")
fmt.Println("----")
fmt.Println("已删除")
fmt.Println("----")
for key, val := range object {
fmt.Printf("key is %s val; is %s", key, val)
fmt.Println("")
}
}
// foreach range 循环
func foreach() {
var list = []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
for index, item := range list {
fmt.Println("index->", index, "item", item)
}
}
// arr 切片
func arr() {
s := []int{1, 2, 3, 4, 5, 6}
fmt.Println("ss", s[2:4])
}
// run gin接口
func run() {
r := gin.Default()
r.GET("/test", func(c *gin.Context) {
c.JSON(200, gin.H{
"data": "success",
})
})
r.GET("/query", func(c *gin.Context) {
name := c.DefaultQuery("name", "taoqun")
age := c.Query("age")
c.JSON(200, gin.H{
"name": name,
"age": age,
})
})
r.GET("/params/:name", func(c *gin.Context) {
name := c.Param("name")
lastname := c.Request.URL.Query().Get("lastname")
message := "name is " + name + " last name is " + lastname
c.String(200, message)
})
// form-data 格式 PostForm
r.POST("/form", func(c *gin.Context) {
name := c.PostForm("name")
age := c.DefaultPostForm("age", "18")
message := "name is " + name + " age is " + age
fmt.Println(message)
c.String(http.StatusOK, message)
})
r.POST("/json", func(c *gin.Context) {
name := c.Request.FormValue("name")
message := "name is " + name
c.String(http.StatusOK, message)
})
r.POST("/upload", func(c *gin.Context) {
file, _ := c.FormFile("file")
c.SaveUploadedFile(file, "upload/"+file.Filename)
fmt.Println(file.Filename)
c.JSON(http.StatusOK, gin.H{
"file": "/upload/" + file.Filename,
})
})
r.POST("/uploadMulti", func(c *gin.Context) {
form, _ := c.MultipartForm()
files := form.File["upload[]"]
var list []string
for _, file := range files {
log.Println(file.Filename)
list = append(list, "upload/"+file.Filename)
c.SaveUploadedFile(file, "upload/"+file.Filename)
}
c.JSON(http.StatusOK, gin.H{
"status": 1,
"data": list,
"len": len(list),
})
})
fmt.Println("http://localhost:8080")
r.Run(":3000")
}