Golang:Go语言中的函数式编程

huangapple go评论95阅读模式
英文:

Golang : functional programming in Go

问题

我尝试了一些我在JavaScript中做的事情。
但是它显示的是:
http://play.golang.org/p/qlWLI03Dnl

    package main

    import "fmt"
    import "regexp"
    import "strings"

    func swapit(str string) string {
    	var validID = regexp.MustCompile(`[a-z]|[A-Z]`)
    	return validID.ReplaceAllString(str, func(
    package main
import "fmt"
import "regexp"
import "strings"
func swapit(str string) string {
var validID = regexp.MustCompile(`[a-z]|[A-Z]`)
return validID.ReplaceAllString(str, func(${0}, ${1}, ${2}) string {
return (${1}) ? strings.ToUpper(${0}) : strings.ToLower(${0})
})
}
func main() {
fmt.Println(swapit("hello wOrld."))
// HELLO WoRLD.
}
, , ) string { return () ? strings.ToUpper(
    package main
import "fmt"
import "regexp"
import "strings"
func swapit(str string) string {
var validID = regexp.MustCompile(`[a-z]|[A-Z]`)
return validID.ReplaceAllString(str, func(${0}, ${1}, ${2}) string {
return (${1}) ? strings.ToUpper(${0}) : strings.ToLower(${0})
})
}
func main() {
fmt.Println(swapit("hello wOrld."))
// HELLO WoRLD.
}
) : strings.ToLower(
    package main
import "fmt"
import "regexp"
import "strings"
func swapit(str string) string {
var validID = regexp.MustCompile(`[a-z]|[A-Z]`)
return validID.ReplaceAllString(str, func(${0}, ${1}, ${2}) string {
return (${1}) ? strings.ToUpper(${0}) : strings.ToLower(${0})
})
}
func main() {
fmt.Println(swapit("hello wOrld."))
// HELLO WoRLD.
}
) }) } func main() { fmt.Println(swapit("hello wOrld.")) // HELLO WoRLD. }

我还尝试了删除? :语法,但仍然不起作用。
http://play.golang.org/p/mD6_78zzo1

Go真的不支持这个吗?我应该放弃,只是用暴力方法逐个字符更改大小写吗?

非常感谢。

英文:

I tried something I did in Javascript.
But it says
http://play.golang.org/p/qlWLI03Dnl

    package main

    import "fmt"
    import "regexp"
    import "strings"

    func swapit(str string) string {
    	var validID = regexp.MustCompile(`[a-z]|[A-Z]`)
    	return validID.ReplaceAllString(str, func(
    package main
import "fmt"
import "regexp"
import "strings"
func swapit(str string) string {
var validID = regexp.MustCompile(`[a-z]|[A-Z]`)
return validID.ReplaceAllString(str, func(${0}, ${1}, ${2}) string {
return (${1}) ? strings.ToUpper(${0}) : strings.ToLower(${0})
})
}
func main() {
fmt.Println(swapit("hello wOrld."))
// HELLO WoRLD.
}
, , ) string { return () ? strings.ToUpper(
    package main
import "fmt"
import "regexp"
import "strings"
func swapit(str string) string {
var validID = regexp.MustCompile(`[a-z]|[A-Z]`)
return validID.ReplaceAllString(str, func(${0}, ${1}, ${2}) string {
return (${1}) ? strings.ToUpper(${0}) : strings.ToLower(${0})
})
}
func main() {
fmt.Println(swapit("hello wOrld."))
// HELLO WoRLD.
}
) : strings.ToLower(
    package main
import "fmt"
import "regexp"
import "strings"
func swapit(str string) string {
var validID = regexp.MustCompile(`[a-z]|[A-Z]`)
return validID.ReplaceAllString(str, func(${0}, ${1}, ${2}) string {
return (${1}) ? strings.ToUpper(${0}) : strings.ToLower(${0})
})
}
func main() {
fmt.Println(swapit("hello wOrld."))
// HELLO WoRLD.
}
) }) } func main() { fmt.Println(swapit("hello wOrld.")) // HELLO WoRLD. }

I also tried this removing ? : syntax but still does not work.
http://play.golang.org/p/mD6_78zzo1

Does really go not support this? Do I just give up and just bruteforce each character to change cases?

Thanks a lot

答案1

得分: 9

如@James Henstridge已经指出,你的代码存在多个问题。本答案不会关注这些错误,而是提供一种不同的解决问题的方法。

如果你的目标是学习如何在Go中使用正则表达式,那么我的回答对你来说是无用的。
如果你的目标是学习如何编写一个交换大小写的函数,我建议使用unicode包而不是正则表达式来解决问题:

package main

import (
	"bytes"
	"fmt"
	"unicode"
)

func SwapCase(str string) string {
	b := new(bytes.Buffer)

	for _, r := range str {
		if unicode.IsUpper(r) {
			b.WriteRune(unicode.ToLower(r))
		} else {
			b.WriteRune(unicode.ToUpper(r))
		}
	}

	return b.String()
}

func main() {
	fmt.Println(SwapCase("Hej värLDen."))
}

输出结果:

hEJ VÄRldEN.

Playground

这个解决方案还可以处理所有非A-Z字符,比如ö-Ö和å-Å。

英文:

As @James Henstridge already pointed out, there are multiple problems with your code. This answer will not focus on the errors, but rather a different way of solving the problem.

If your aim is to learn about using regexp in Go, this answer of mine is useless.
If your aim is to get learn how to make a function that swaps cases, then I suggest a solution without regexp, utilizing the unicode package instead:

package main

import (
	"bytes"
	"fmt"
	"unicode"
)

func SwapCase(str string) string {
	b := new(bytes.Buffer)

	for _, r := range str {
		if unicode.IsUpper(r) {
			b.WriteRune(unicode.ToLower(r))
		} else {
			b.WriteRune(unicode.ToUpper(r))
		}
	}

	return b.String()
}

func main() {
	fmt.Println(SwapCase("Hej värLDen."))
}

Output:
> hEJ VÄRldEN.

Playground

This solution will handle all non A-Z characters as well, such as ö-Ö and å-Å.

huangapple
  • 本文由 发表于 2013年10月2日 10:42:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/19129207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定