声明全局变量的时候注意一下好不

before

Go 复制代码
package router

import (
	"IM_for_imageProcess/api-01/utitl"
	"encoding/json"
	//"errors"
	"fmt"
	"net/http"
	"time"
)

type History struct {
}

//var senderIdd int
//var receiverIdd int
//var messageContent string
//var createAt time.Time
//var isReceived string
type HistoryRes struct{
	SenderIdd int `json:"sender_idd"`
	ReceiverIdd int `json:"receiver_idd"`
	MessageContent string `json:"message_content"`
	CreateAt time.Time `json:"create_at"`
	IsReceived string `json:"is_received"`

}
var HistoryResList []HistoryRes

func reverse(arr []HistoryRes)  {

	i:=0
	j:=len(arr)-1

	for{
		if i < j{
			arr[i],arr[j]=arr[j],arr[i]
			i=i+1
			j=j-1
		} else{
			break
		}


	}



}







func (this *History) GetHistoryMessage(w http.ResponseWriter, r *http.Request) {
	err := r.ParseForm()
	if err != nil {
		return
	}
	w.Header().Set("Access-Control-Allow-Origin", "*")

	w.Header().Set("Access-Control-Allow-Credentials", "true")


	requestMap := r.Form

	senderId := requestMap["senderId"][0]

	receiverId := requestMap["receiverId"][0]
	//var HistoryResList []HistoryRes



	//Id             | int(11)    | NO   | PRI | NULL    | auto_increment |
	//| senderId       | int(11)    | YES  |     | NULL    |                |
	//| receiverId     | int(11)    | YES  |     | NULL    |                |
	//| messageContent | text       | YES  |     | NULL    |                |
	//| createAt       | datetime   | YES  |     | NULL    |                |
	//| isReceived     | varchar(3) | YES  |     | yes     |                |




	//rows, err := utitl.Db.Query("select senderId,receiverId,messageContent,createAt,isReceived from MessageTable where senderId = ? and receiverId=? or receiverId=? and senderId=? order by createAt desc limit 15", senderId,receiverId,receiverId,senderId)

	rows, err := utitl.Db.Query("select senderId,receiverId,messageContent,createAt,isReceived from MessageTable where senderId = ? and receiverId=? or receiverId=? and senderId=? order by createAt desc limit 15", senderId,receiverId,senderId,receiverId)

	if err != nil {
		fmt.Println(fmt.Sprintf("select tweetId from Tweet error: %v\n", err))
		//return nil,errors.New(fmt.Sprintf("select tweetId from Tweet error: %v\n", err))
	}
	var senderIdd int
	var receiverIdd int
	var messageContent string
	var createAt time.Time
	var isReceived string
	for rows.Next() {
		err := rows.Scan(&senderIdd,&receiverIdd, &messageContent, &createAt,&isReceived)
		if err != nil {
			fmt.Printf("scan failed, err:%v\n", err)
			//return nil,nil ,err
			//return nil, err
			//return nil,err
		}

		historyRes:=HistoryRes{
			SenderIdd:senderIdd,
			ReceiverIdd:receiverIdd,
			MessageContent:messageContent,
			CreateAt:createAt,
			IsReceived:isReceived,
		}
		//tweetDetail := model.TweetDetail{
		//	TweetId:  tweetId,
		//	UserId:   star,
		//	Content:  content,
		//	CreateAt: createAt,
		//	PostTime:postTime,
		//}
		//哈哈 这个就是我一个朋友的推文
		//common.MapTweetDetail[createAt]=tweetDetail
		HistoryResList=append(HistoryResList,historyRes)

		//fmt.Println("tweetDetail=====",tweetDetail)
	}

	fmt.Println("len(HistoryResList)===",len(HistoryResList))

	reverse(HistoryResList)

	//sort.Stable(sort.Reverse(sort.IntSlice(HistoryResList)))


	HistoryResListMar,err:=json.Marshal(HistoryResList)




	//timeElapse:=time.Since(timeStart)
	//fmt.Println("timeElapse=====",timeElapse)
	//timeElapse=rpc获取friendsList +从tweettable 中获取明星的信息 +从news_feed表中获取普通人的信息+对两个表的信息排序

	fmt.Fprintf(w,string(HistoryResListMar))

}

after:

after:

Go 复制代码
package router

import (
	"IM_for_imageProcess/api-01/utitl"
	"encoding/json"
	//"errors"
	"fmt"
	"net/http"
	"time"
)

type History struct {
}

//var senderIdd int
//var receiverIdd int
//var messageContent string
//var createAt time.Time
//var isReceived string
type HistoryRes struct{
	SenderIdd int `json:"sender_idd"`
	ReceiverIdd int `json:"receiver_idd"`
	MessageContent string `json:"message_content"`
	CreateAt time.Time `json:"create_at"`
	IsReceived string `json:"is_received"`

}
//var HistoryResList []HistoryRes

func reverse(arr []HistoryRes)  {

	i:=0
	j:=len(arr)-1

	for{
		if i < j{
			arr[i],arr[j]=arr[j],arr[i]
			i=i+1
			j=j-1
		} else{
			break
		}


	}



}







func (this *History) GetHistoryMessage(w http.ResponseWriter, r *http.Request) {
	err := r.ParseForm()
	if err != nil {
		return
	}
	w.Header().Set("Access-Control-Allow-Origin", "*")

	w.Header().Set("Access-Control-Allow-Credentials", "true")


	requestMap := r.Form

	senderId := requestMap["senderId"][0]

	receiverId := requestMap["receiverId"][0]
	var HistoryResList []HistoryRes



	//Id             | int(11)    | NO   | PRI | NULL    | auto_increment |
	//| senderId       | int(11)    | YES  |     | NULL    |                |
	//| receiverId     | int(11)    | YES  |     | NULL    |                |
	//| messageContent | text       | YES  |     | NULL    |                |
	//| createAt       | datetime   | YES  |     | NULL    |                |
	//| isReceived     | varchar(3) | YES  |     | yes     |                |




	//rows, err := utitl.Db.Query("select senderId,receiverId,messageContent,createAt,isReceived from MessageTable where senderId = ? and receiverId=? or receiverId=? and senderId=? order by createAt desc limit 15", senderId,receiverId,receiverId,senderId)

	rows, err := utitl.Db.Query("select senderId,receiverId,messageContent,createAt,isReceived from MessageTable where senderId = ? and receiverId=? or receiverId=? and senderId=? order by createAt desc limit 15", senderId,receiverId,senderId,receiverId)

	if err != nil {
		fmt.Println(fmt.Sprintf("select tweetId from Tweet error: %v\n", err))
		//return nil,errors.New(fmt.Sprintf("select tweetId from Tweet error: %v\n", err))
	}
	var senderIdd int
	var receiverIdd int
	var messageContent string
	var createAt time.Time
	var isReceived string
	for rows.Next() {
		err := rows.Scan(&senderIdd,&receiverIdd, &messageContent, &createAt,&isReceived)
		if err != nil {
			fmt.Printf("scan failed, err:%v\n", err)
			//return nil,nil ,err
			//return nil, err
			//return nil,err
		}

		historyRes:=HistoryRes{
			SenderIdd:senderIdd,
			ReceiverIdd:receiverIdd,
			MessageContent:messageContent,
			CreateAt:createAt,
			IsReceived:isReceived,
		}
		//tweetDetail := model.TweetDetail{
		//	TweetId:  tweetId,
		//	UserId:   star,
		//	Content:  content,
		//	CreateAt: createAt,
		//	PostTime:postTime,
		//}
		//哈哈 这个就是我一个朋友的推文
		//common.MapTweetDetail[createAt]=tweetDetail
		HistoryResList=append(HistoryResList,historyRes)

		//fmt.Println("tweetDetail=====",tweetDetail)
	}

	fmt.Println("len(HistoryResList)===",len(HistoryResList))

	reverse(HistoryResList)

	//sort.Stable(sort.Reverse(sort.IntSlice(HistoryResList)))


	HistoryResListMar,err:=json.Marshal(HistoryResList)




	//timeElapse:=time.Since(timeStart)
	//fmt.Println("timeElapse=====",timeElapse)
	//timeElapse=rpc获取friendsList +从tweettable 中获取明星的信息 +从news_feed表中获取普通人的信息+对两个表的信息排序

	fmt.Fprintf(w,string(HistoryResListMar))

}
相关推荐
幼儿园老大*2 小时前
Go的环境搭建以及GoLand安装教程
开发语言·经验分享·后端·golang·go
童先生9 小时前
Go 项目中实现类似 Java Shiro 的权限控制中间件?
开发语言·go
幼儿园老大*10 小时前
走进 Go 语言基础语法
开发语言·后端·学习·golang·go
架构师那点事儿15 小时前
golang 用unsafe 无所畏惧,但使用不得到会panic
架构·go·掘金技术征文
于顾而言1 天前
【笔记】Go Coding In Go Way
后端·go
qq_172805591 天前
GIN 反向代理功能
后端·golang·go
follycat2 天前
2024强网杯Proxy
网络·学习·网络安全·go
OT.Ter2 天前
【力扣打卡系列】单调栈
算法·leetcode·职场和发展·go·单调栈
探索云原生2 天前
GPU 环境搭建指南:如何在裸机、Docker、K8s 等环境中使用 GPU
ai·云原生·kubernetes·go·gpu
OT.Ter2 天前
【力扣打卡系列】移动零(双指针)
算法·leetcode·职场和发展·go