Golang using MONGO

Source: Internet
Author: User
Tags parse string
This is a creation in Article, where the information may have evolved or changed.

Generate Objectid

i := bson.NewObjectId()c.Insert(bson.M{"_id": i, "foo": "bar"})

Golang using MONGO GTE LTE function

searchErr = SearchReading(bson.M{"k": key, "t": bson.M{"$gte": start, "$lte": end}}, limit)
searchErr = SearchReading(bson.M{"k": key, "t":'{"$gte": start, "$lte": end}'}, limit)

Basic Gadgets

Golang MD5 Calculation

Func genQueryMd5 (str string) string {

Hash: = MD5. New ()

B: = []byte (str)

Hash. Write (b)

Hash. Sum (Nil)

Return to FMT. Sprintf ("%x", MD5. Sum (b))

}

    1. When you call sum (nil) it returns D.checksum () directly as a byte slice, however if you call Sum ([]byte) it appends d.  CheckSum () to your input.
    2. The Write function will clear the string inside the MD5 object and use its arguments as the new internal string. The SUM function computes the MD5 value of the internal string and appends the input parameter to the internal string. Can think: hash. Write (b) + hash. Sum (nil) = hash. Write (nil) + hash. Sum (b) + hash. Sum (nil) = MD5. Sum (b).

Parse string as Int64
Func parseint (s string, base int, bitsize int) (I int64, err error)

The string form of the parameter 1 number

Parameter 2 the binary octal decimal hexadecimal of a numeric string, such as binary

Parameter 3 returns the bit size of the result, which is int8 int16 int32 int64

Import

(    "StrConv")
I, err: = StrConv. parseint("123", 10, 32)


Time Stamp Interchange
Gets the timestamp with time. Now (). Unix (), formatted time with T. Format, time for parsing. Parse
Timestamp: = time. Now (). Unix ()
Formatted as a string, TM is Time type TM: = time. Unix (timestamp, 0) fmt. Println (TM. Format ("2006-01-02 03:04:05"))
From a string to a timestamp, the first argument is the format, the second is the time string to convert tm2, _: = times. Parse ("01/02/2006", "02/08/2015")

Golang byte to string

Golang inside the concept of string is not actually encountered/0 end of the concept, he is actually a continuous memory, the first address + length, the above assignment, if p inside has/0, he will not do this time, if the string to do other processing may be a problem, Like StrConv. Atoi to int there is an error, the solution is to write a formal conversion function:

Func byteString (P []byte) string {

For I: = 0; i < Len (p); i++ {

If p[i] = = 0 {

return string (P[0:i])

}

}

return string (p)

}

Golang polymorphism's support. Interface support for polymorphism is still relatively limited. Down casting can not do sometimes is not very convenient. Using the parent class to do the method parameter, passing the subclass will compile the failure, cannot do the general class

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.