How to get IP address in Go:Beego

1.5k Views Asked by At

I have an beego application where I need to get the client side IP address and send it to server in same format or string format.

How can I get the IP address of the client so that I can send it to server and display on server side.

    l_channel_ip := "10.11.0.123"

Here I am hard coding the value now. But I don't want it to be hard coded like this. Instead the clients IP should be stored in l_channel_ip.

2

There are 2 best solutions below

1
On

This code stores the ip in "l_channel_ip" variable

func (this *baseController) getClientIp() string {
s := strings.Split(this.Ctx.Request.RemoteAddr, ":")
return s[0]
}

l_channel_ip := getClientIp()
0
On

This code provide IP address for you

s := this.Ctx.Input.IP()

Use beego internal istead custom parsing.