欢迎来到 艾瑞可erik 的魔法世界 🧙‍♂️

给我一个需求,还你一个项目——别管它是什么。这就是全栈。

📚 291 篇文章 🏷️ 504 个标签 📂 90 个分类

🔥 最新文章

go中获取HTTP请求的IP地址

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
}

Getting the IP Address of an HTTP Request in Go

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
}

go中限流器

限流器是后台服务中的非常重要的组件,可以用来限制请求速率,保护服务,以免服务过载。 限流器的实现方法有很多种,例如滑动窗口法、Token Bucket、Leaky Bucket等。

其实golang标准库中就自带了限流算法的实现,即golang.org/x/time/rate。 该限流器是基于Token Bucket(令牌桶)实现的。

简单来说,令牌桶就是想象有一个固定大小的桶,系统会以恒定速率向桶中放Token,桶满则暂时不放。 而用户则从桶中取Token,如果有剩余Token就可以一直取。如果没有剩余Token,则需要等到系统中被放置了Token才行。

Rate limiters in Go

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.

new和make的区别

go中我们往往要对变量分配内存,那么分配内存有两中方式分别是new和make。查了一些资料,顺便看我开发中用的几个地方。其实,很好理解。

new分配中内存,不做初始化,也就是不能直接赋值,必须初始化后才能赋值。

make分配好内存且已经

The Difference Between new and 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

结构体转map

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

以上就是转化过程。

Converting a Struct to a map

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.

hyperf异常

  • Maximum function nesting level of ‘256’ reached, aborting

    先跑了所有接口,发现都是这个报错。梳理新增加的接口方法后,直接屏蔽新接口所有方法逻辑,就恢复正常。依次排除法,最终发现在model里面封装的方法调用了service层的方法导致超负荷加载函数嵌套。
    

Hyperf Exceptions

  • 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.