小编cam*_*beh的帖子

Scala中的简单函数getopt

def main(args: Array[String]) {
  if (args.length == 0) println(usage)
  val argList = args.toList
  type OptionMap = Map[Symbol, Any]

  def nextOption(map: OptionMap, list: List[String]): OptionMap = {
    list match {
      case Nil => map
      case "-h" | "--help" :: tail => usage(); sys.exit(0)
      case "-p" | "--port" :: option :: tail => nextOption(map ++ Map('port -> option.toInt), tail)
  }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法捕获更多的头部价值List?此代码生成

type mismatch;
   found   : String("-h")
   required: List[String]
        case "-h" | "--help" :: tail => usage(); sys.exit(0)
             ^
Run Code Online (Sandbox Code Playgroud)

可能重复: …

scala getopt

1
推荐指数
1
解决办法
846
查看次数

Go中的不可变字符串

有人能够解释我为什么地址&c1.name在功能改变后是相同的changeMe().我认为Go中的字符串是不可变的.

package main

import "fmt"

type customer struct {
    name string
    age  int
}

func main() {
    c1 := customer{"Todd", 44}
    fmt.Println(&c1.name) // 0x8201e4120

    changeMe(&c1)

    fmt.Println(c1)       // {Rocky 44}
    fmt.Println(&c1.name) // 0x8201e4120
}

func changeMe(z *customer) {
    fmt.Println(z)       // &{Todd 44}
    fmt.Println(&z.name) // 0x8201e4120
    z.name = "Rocky"
    fmt.Println(z)       // &{Rocky 44}
    fmt.Println(&z.name) // 0x8201e4120
}
Run Code Online (Sandbox Code Playgroud)

string pointers immutability go

1
推荐指数
1
解决办法
451
查看次数

标签 统计

getopt ×1

go ×1

immutability ×1

pointers ×1

scala ×1

string ×1