Golang string cut function split

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

First, the conclusion: The big Golang inside, if an empty string is strings cut through the function of the packet, Split then the result is an array of length 1, the contents of which is an empty string.

In order to verify, respectively in 1.0.1, 1.1, 1.2.2, 1.3.3, 1.4RC above test, verified the above conclusion is correct.

func main() {a := strings.Split("", ";")fmt.Printf("%d****%s****\n", len(a), a[0])}

This is also very well understood. Cut an empty string, there must be no way to cut, then the result is not cut, the original string directly into the result array. It's just a bit hard to understand at first, because we all think that if it's an empty string, the result array should be empty.

Recently upgraded the beego1.4.2, matchmaker the Beego controller to get the request parameter function Controller.GetInt function return value, from the previous int64 change int to, originally the first time on the awkward, if want to take int64 , that directly add a function Controller.GetInt64 on it can be, As a result, you have to think about it. As a result, the matchmaker decisively Controller.GetInt changed the return value, the project was compiled directly.

Of course, this is nothing, compile but at least know where the problem is. There is also a pit, beego.AppConfig.Strings function, which can be used to automatically cut the strings in the configuration into groups by semicolons. After the result is updated, the previous code return is empty (that is, an empty string with a length of 1). This is killing me. Looked under the source code, found the Config package resolution configuration file method has been greatly updated, but I have been analyzed, I think it is not the error here, but another place beego/config.go 111th line

func (b *beegoAppConfig) Strings(key string) []string {v := b.innerConfig.Strings(RunMode + "::" + key)if len(v) == 0 {return b.innerConfig.Strings(key)}return v}

runmodegets the configured value from the, if not fetched, the default value. In this case, Beego configuration file support dev , prod and default three modes. If configured runmode , to find the appropriate configuration within the configuration file, otherwise go to the default value. The above code is basically the meaning. But if the runmode next configuration is not found, then if the judgment is len(v) == 0 , and the function that previously took the value from the configuration file implementation is ini.go as follows:

func (c *IniConfigContainer) Strings(key string) []string {return strings.Split(c.String(key), ";")}

It will take the string to cut, combined with what we mentioned above, no matter how to cut, the length is at least 1, so the if judgment is always false , take the default value of the logic will never be executed. The modified method is also very simple, the judgment language if v[0] == "" can be changed. The completed Beego/config package introduction can be seen in my previous article. I've already given matchmaker the merge Request, but he hasn't been calling me ...

No. 24th Xie Da merged My Code, happy and happy.

Please refer to the complete source code in this article.

Original link: Golang string cutting function split, reproduced please indicate the source!

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.