go中类型转换的搜集
func StringToInt(a string) int {
d, _ := strconv.Atoi(a)
return d
}
给我一个需求,还你一个项目——别管它是什么。这就是全栈。
go中类型转换的搜集
func StringToInt(a string) int {
d, _ := strconv.Atoi(a)
return d
}
A collection of type conversions in Go
func StringToInt(a string) int {
d, _ := strconv.Atoi(a)
return d
}
一个简单但是非常实用的pdf生成器!
安装:
go get github.com/jung-kurt/gofpdf
A simple but very practical PDF generator!
Install:
go get github.com/jung-kurt/gofpdf
package main
import (
"encoding/json"
"net/http"
)
func main() {
http.HandleFunc("/", ExampleHandler)
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
func ExampleHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
resp, _ := json.Marshal(map[string]string{
"ip": GetIP(r),
})
w.Write(resp)
}
func GetIP(r *http.Request) string {
forwarded := r.Header.Get("X-FORWARDED-FOR")
if forwarded != "" {
return forwarded
}
return r.RemoteAddr
}
package main
import (
"encoding/json"
"net/http"
)
func main() {
http.HandleFunc("/", ExampleHandler)
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
func ExampleHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
resp, _ := json.Marshal(map[string]string{
"ip": GetIP(r),
})
w.Write(resp)
}
func GetIP(r *http.Request) string {
forwarded := r.Header.Get("X-FORWARDED-FOR")
if forwarded != "" {
return forwarded
}
return r.RemoteAddr
}
限流器是后台服务中的非常重要的组件,可以用来限制请求速率,保护服务,以免服务过载。 限流器的实现方法有很多种,例如滑动窗口法、Token Bucket、Leaky Bucket等。
其实golang标准库中就自带了限流算法的实现,即golang.org/x/time/rate。 该限流器是基于Token Bucket(令牌桶)实现的。
简单来说,令牌桶就是想象有一个固定大小的桶,系统会以恒定速率向桶中放Token,桶满则暂时不放。 而用户则从桶中取Token,如果有剩余Token就可以一直取。如果没有剩余Token,则需要等到系统中被放置了Token才行。
A rate limiter is a very important component in backend services. It can be used to limit the request rate and protect the service from being overloaded. There are many ways to implement a rate limiter, such as the sliding window method, Token Bucket, Leaky Bucket, and so on.
In fact, the Go standard library already includes an implementation of a rate limiting algorithm, namely golang.org/x/time/rate. That limiter is implemented on top of Token Bucket.
Put simply, a token bucket is a bucket of fixed size: the system puts tokens into the bucket at a constant rate, and stops for the moment once the bucket is full. Users take tokens out of the bucket, and as long as there are tokens left they can keep taking them. If no tokens are left, they have to wait until the system has placed more into the bucket.
go中我们往往要对变量分配内存,那么分配内存有两中方式分别是new和make。查了一些资料,顺便看我开发中用的几个地方。其实,很好理解。
new分配中内存,不做初始化,也就是不能直接赋值,必须初始化后才能赋值。
make分配好内存且已经
In Go we often have to allocate memory for variables, and there are two ways to allocate memory: new and make. I looked up some material, and took the chance to review the places where I use them in my own development. Actually, it’s quite easy to understand.
new allocates memory but does not initialize it, which means you cannot assign to it directly — you must initialize it before you can assign.
make allocates the memory and has already
在go中往redis的hash写数据的时候遇到了结构体数组写入时无法写入,看了看写入数据要求是map[string]interface,而我的是struct。因此,无法写入的。
那么就是转格式呗
type order struct{
Id int64 `json:"id"`
orderSn int64 `json:"order_sn"`
}
orders:=order{
Id:2022032034566
OrderSn:20220320122444
}
jsonData,_:=json.Marshal(orders)
var redisData map[string]interface{}
json.Unmarshal([]byte(jsonData),&redisData)
log.Info(redisData)
以上就是转化过程。
When writing data to a redis hash in go, I ran into the problem that a struct array could not be written. I looked into it and the data to be written is required to be map[string]interface, while mine was a struct. Therefore, it could not be written.
So it’s just a matter of converting the format.
type order struct{
Id int64 `json:"id"`
orderSn int64 `json:"order_sn"`
}
orders:=order{
Id:2022032034566
OrderSn:20220320122444
}
jsonData,_:=json.Marshal(orders)
var redisData map[string]interface{}
json.Unmarshal([]byte(jsonData),&redisData)
log.Info(redisData)
The above is the conversion process.
Maximum function nesting level of ‘256’ reached, aborting
先跑了所有接口,发现都是这个报错。梳理新增加的接口方法后,直接屏蔽新接口所有方法逻辑,就恢复正常。依次排除法,最终发现在model里面封装的方法调用了service层的方法导致超负荷加载函数嵌套。
Maximum function nesting level of ‘256’ reached, aborting
I ran all the interfaces first and found they all threw this error. After going through the newly added interface methods, I simply disabled all the method logic in the new interface and everything went back to normal. Eliminating them one by one, I finally found that a method encapsulated in the model called a method in the service layer, which caused an overload of nested function calls.
当一个项目沉淀一段时间后,随着用户量的提升,用户群体庞大起来。这时候就会出现过多的用户访问导致服务器(单机)供应不足。那么,负载均衡就出现了。
Once a project has been running for a while, its user base grows along with the traffic. At that point, too many user requests start to overwhelm the server (a single machine) and it can no longer keep up. That’s where load balancing comes in.
谈起Redis锁,下面三个,算是出现最多的高频词汇:
Setnx
其实目前通常所说的Setnx命令,并非单指Redis的setnx key value这条命令。
When it comes to Redis locks, these three are probably the most frequently mentioned terms:
Setnx
Actually, what people usually call the Setnx command doesn’t only mean Redis’s setnx key value command.
没有找到匹配的文章 🙈