This is a creation in Article, where the information may have evolved or changed.
Go third-party library installation
Set environment variable Gopath=c:\go\code
Create a source code directory under C:\go\code src
Enter C:\GO\CODE\SRC
Install third-Party library commands
git clone git://github.com/alphazero/go-redis.git Redis
Installation of Redis can be performed in any directory
Go Install Redis
After installation, generate a pkg directory under the C:\go\code directory and generate the REDIS.A in the directory.
You can generate a catalog file under C:\go\code\src xn
The structure is as follows
The file contents are as follows
Xn.go
Package xn
Import (
"FMT"
)
func Helloworld () {
Fmt. Println (" helloeorld function within the xn package ")
}
func xx () {
Fmt. Println (" xx function in xn package ")
}
Neng.go files are as follows
Package Neng
Import (
"FMT"
)
func Helloworld () {
Fmt. Println (" helloeorld function within the Xn.neng package ")
}
Installing XN third-party libraries
Go install xn
Examples of Use
Package Main
Import (
"FMT"
"Log"
"Redis"
"xn"
"Xn/neng"
)
func Main () {
b:= 2 //:= This notation can only be used inside a function
Fmt. Println (b)
Fmt. Println ("Hello,world, Hello, World ")
Xn. Helloworld ()
Neng. Helloworld ()
//defaultspec () Create a connection specification
Spec: = Redis. Defaultspec (). Host ("127.0.0.1"). Port (6666). Password ("test123");
// Create a new syncclient , and connect to Redis on the server using the , Specify Connectionspec interface.
Client, Err: = Redis. Newsynchclientwithspec (spec);
if err! =Nil {
Log. Println (" connection Server failed >", Err);
return
}
Dbkey: = "GAME:TEST:info";
Value, Err: = client. Get (Dbkey);
if err!= nil {
Log. PRINTLN ("error on Get", err);
return
}
//
if value = =Nil {
Value: =[]byte("sina.com!");
Client. Set (Dbkey, value);
Fmt. Printf (" Insert data >%s \ n", value)
} Else {
Fmt. Printf (" received data >%s \ n", value);
//return;
}
}