Go third-party Library installation Example

Source: Internet
Author: User
Tags install redis
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;

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.