dongrui6787 2013-08-11 22:23
浏览 31
已采纳

缩短Go / Golang中的进口变量出口?

if I have

// types.go

type S string

func (s *S) Lower() *S {
    *s = S(strings.ToLower(string(*s)))
    return s
}

`

// in another file

import "u/types"

func main() {
    s := types.S("asdf")
    if s == "asdf" {
        s.Lower()
    }
}
  1. Is there a way to shorten types.S("asdf") to just S("asdf")?

  2. Is there a way to lowercase method calls from other files? e.g. s.Lower() => s.lower()?

  • 写回答

2条回答 默认 最新

  • doucai9270 2013-08-11 22:45
    关注

    It's not recommended for most cases but you can do import . "u/types" and all then skip the types prefix. . will import all the public symbols into your package for you allowing you to call them as if they were local to your package.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?