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

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))

}
相关推荐
DemonAvenger5 小时前
深入剖析 sync.Once:实现原理、应用场景与实战经验
分布式·架构·go
一个热爱生活的普通人1 天前
Go语言中 Mutex 的实现原理
后端·go
孔令飞1 天前
关于 LLMOPS 的一些粗浅思考
人工智能·云原生·go
小戴同学1 天前
实时系统降低延时的利器
后端·性能优化·go
Golang菜鸟2 天前
golang中的组合多态
后端·go
Serverless社区2 天前
函数计算支持热门 MCP Server 一键部署
go
Wo3Shi4七2 天前
二叉树数组表示
数据结构·后端·go
网络研究院2 天前
您需要了解的有关 Go、Rust 和 Zig 的信息
开发语言·rust·go·功能·发展·zig
27669582922 天前
拼多多 anti-token unidbg 分析
java·python·go·拼多多·pdd·pxx·anti-token
程序员爱钓鱼3 天前
Go 语言邮件发送完全指南:轻松实现邮件通知功能
后端·go·排序算法