loadtest/src/client/client.go
import (
"fmt"
"net"
"bufio"
"log"
)
type Client struct {
Conn net.Conn
Err error
}
func (e Client) SetConnection(ServerAddr string){
e.Conn, e.Err = net.Dial("tcp", ServerAddr)
if e.Err != nil {
log.Fatalf("Fail to connect to Server")
}
}
func (e Client) MakeRequest(Message string){
e.Conn.Write([]byte(Message))
}
func (e Client) ListenResponse() {
message, _ := bufio.NewReader(e.Conn).ReadString('}')
fmt.Print("Message from server: " + message + "
")
}
loadtest/src/main/main.go
package main
import (
"client"
)
func main() {
e:=client.Client{}
e.SetConnection("192.168.0.1:3999")
e.MakeRequest("login_req") //panic: runtime error: invalid memory address or nil pointer dereference
e.ListenResponse()
}
I use above code to create and initialize the Client. But when the MakeRequest is called, It print runtime errors invalid memory address or nil pointer dereference, and it noticed me e.Conn is "nil" and e.Err is also "nil".
I want to initialize instance of the struct 'Client' with SetConnection. How can I do that?