This is the fifth day of my participation in the August More text Challenge. For details, see:August is more challenging
♟ Redis connection and data manipulation for Go language things ♟
I. Install the required third-party packages
Go get github.com/garyburd/redigo/redis in command line mode execution
You can download the package to the GOPATH directory.
2. Start redis-server and redis-CLI
Remember to add the configuration file when starting the server
redis-server .\redis.windows.conf
Copy the code
Start the client. If there is a password, enter the password.
C:\Users\11316\Desktop\redis-latest>redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379>
Copy the code
Write code
package main import ( "github.com/garyburd/redigo/redis" "fmt" "os" ) func HandleError(err error,when string) { if err! =nil{ fmt.Println(when,err) os.Exit(1) } } func main() { // Dial connects to the Redis server at the given network and // address using the specified options. conn, err := redis.Dial("tcp", "127.0.0.1:6379") HandleError(err," redis.dial ") // Defer conn.close () // Do send a command to the server and Reply, err := conn.Do("Get", "Name ") HandleError(err," conn.do Get") fmt.Printf("type=%T value=%v",reply,reply) _ := redis.String(reply, err) fmt.Println(nameStr) }Copy the code
First, you import the packages.
And then I wrote a function to handle errors. Os.exit (1) indicates an error, the program ends immediately, and what was deferred is not executed again.
Redis.dial () this function is used to connect to Redis. You need to specify the network protocol, IP address and port number. The default redis port number is 6379.
Defer conn.close () : end the connection with Redis as follows: In order to save SYSTEM I/O resources, end it in time! It’s easy for beginners to forget this. Make it a habit!
Conn.do () is used to execute the database command. The first argument is the command name and the following arguments are the parameters of the database command. Reply is a byte array []byte that needs to be converted according to the specific business type.
Let’s understand the Do() function
Replace conn.do () with the following:
reply, err := conn.Do("llen", "list")
Copy the code
The execution result is:
type=int64 value=5
Copy the code
It is found that the output is not correctly printed, the output is empty string, that is, the output of reply is not successful.
Then we change the data type conversion function.
Use:
nameStr, _ := redis.Int(reply, err)
Copy the code
Since the query result is of type Int, our previous conversion to String was unsuccessful. Here we now use Int() to convert the output successfully.
Let’s see how it works!
type=int64 value=5
5
Copy the code
Similarly, if it’s a Float type we need to use Float(), and if it’s a type we need to convert it to what type, so that we can print it correctly.
Operational data
Set operations
reply, err := conn.Do("SET", "myname","ReganYue")
Copy the code
Running result:
type=string value=OK
OK
Copy the code
That’s what we need
redis.String()
Copy the code
To convert data of type byte[] to string data for correct output.
reply, err := conn.Do("setex","yourname",20,"hello")
Copy the code
As shown above, use multiple arguments if you have more than one argument, which is to turn the Spaces in the command line into commas, and enclose strings in double quotes, but not strings in double quotes.
The above code execution result:
type=string value=OK
OK
Copy the code
127.0.0.1:6379> ttl yourname
(integer) 5
127.0.0.1:6379> ttl yourname
(integer) 2
127.0.0.1:6379> ttl yourname
(integer) 1
127.0.0.1:6379> ttl yourname
(integer) -2
Copy the code
The execution is successful!
Redis. String () and redis. Strings ()
reply, err := conn.Do("lrange","list",0,-1)
Copy the code
If this statement is executed, then redis.strings () can be used
nameStr, _ := redis.Strings(reply, err)
Copy the code
The result is this:
type=[]interface {} value=[[49] [110 97 109 101] [51] [50] [49]]
[1 name 3 2 1]
Copy the code
If you use redis.string (), the result is this:
type=[]interface {} value=[[49] [110 97 109 101] [51] [50] [49]]
Copy the code
Come and see what the difference is!
This is where we need to be aware that using redis.string () will result in no output.
Redis supports pipework, so you can use pipework
Pipelinization operations include Send(), Flush(), and Receive() methods
The Do method combines the Send,Flush, and Receive methods.