Giter Club home page Giter Club logo

python2.7-to-golang's Introduction

python2.7-to-golang

Retire python2.7 scripts and try to migrate to golang.

Type

string

  • "aaabbb".startswith("aa")
strings.HasPrefix("aaabbb", "aa")
  • "aaabbb".endswith("bb")
strings.HasSuffix("aaabbb", "bb")
  • "a"*16
strings.Repeat("a", 16)
  • "a" in "aa"
strings.Contains("aa", "a")
  • "abcb".replace("b", "c")
strings.ReplaceAll("abcb", "b", "c")
  • "1|2|3".split("|", 1)

Notice that 1 means split the string by at most 1 |, however split function in golang use the number of parts as a parameter.

strings.SplitN("1|2|3", "|", 2)

float(?)

Python's float function tranforms many things to float. According to this, "infinity" can also be tranformed to float.

Encode to utf-8???

No need.

Package

threading

  • threading.Lock: Golang's sync.Mutex only provides Lock and Unlock operation.
// A Lock
ch := make(chan bool, 1)

// Lock
ch <- true

// Trylock?
select {
case ch <- true:
    // Trylock successfully
default:
    // Fail to lock 
}

// Islock?
len(ch) == 1

// Unlock
<-ch

string

  • string.Template("${a}${b}").safe_substitute({"a":1,"b":2})

This function doesn't has a proper replace go package. We need to implement PEP0292 ourself. strings.Replacer will apply to most of the cases.

func SafeSubstitute(temp string, vars map[string]string) string {
	replacerList := []string{}
	for varName, varValue := range vars {
		replacerList = append(replacerList, "${"+varName+"}")
		replacerList = append(replacerList, varValue)
		replacerList = append(replacerList, "$"+varName)
		replacerList = append(replacerList, varValue)
	}
	replacerList = append(replacerList, "$$")
	replacerList = append(replacerList, "$")
	rep := strings.NewReplacer(replacerList...)
	return rep.Replace(temp)
}

time

  • int(time.time()): Unix second
time.Now().Unix()

urllib

  • urllib.quote(s): Quote path section of the URL
url.PathEscape(s)
  • urllib.quote_plus(s): Quote query section of the URL
url.QueryEscape(s)

urlparse

  • urlparse.urlparse
    • u = urlparse('http://www.aaa.bb:80/c/d.html?a=1')

      • u.schema: http
      • u.netloc: www.aaa.bb:80
      • u.hostname: www.aaa.bb
      • u.query: a=1
    • u, _ := url.Parse("http://www.aaa.bb:80/c/d.html?a=1")

      • u.Schema: http
      • u.Host: www.aaa.bb:80
        • host, port, _ := net.SplitHostPort(u.Host)
          • host: www.aaa.bb
          • post: 80
          • Notice that net.SplitHostPort("www.kimo.com") return err and empty host and empty port
      • u.RawQuery: a=1

os

  • os.path.join(a, b, c)
path.Join(a, b, c)

base64

URL Encoding

  • base64.urlsafe_b64encode(s)

Example: base64.urlsafe_b64encode("a") = "YQ=="

base64.RawURLEncoding.EncodeToString([]byte("a"))

EncodeToString("a") return "YQ" which has no "=" padding.

URL Decoding

  • base64.urlsafe_b64decode(s)

Example: base64.urlsafe_b64decode("YQ==") = "a" Throw exception if no "=" padding

base64.RawURLEncoding.DecodeToString([]byte("YQ"))

DecodeToString("YQ") return "a". DecodeToString("YQ==") return error: "=" is an illegal character.

pytricia

Support ipv4, ipv6

python2.7-to-golang's People

Contributors

mudream4869 avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.