如何在Go中创建私有方法或结构

时间:2019-07-18 06:08:00

标签: go

我曾经使用OOP类型的编程进行编码,我想知道封装如何在功能编程语言(如go)中工作。

type Message struct{
   value string
}
func Greet Hello{
  msg := &Message{value: "How are you"}
  fmt.Println(msg.value)
}

给出以下代码,如何知道结构和方法是私有的还是公共的?

5 个答案:

答案 0 :(得分:5)

Go有一个有趣的系统。在Go中,函数和类型是“导出”或“未导出”的(其他语言为“公共”或“私有”)。要知道是否导出了函数或类型,只需检查其名称的第一个字母即可。如果首字母大写,则将导出函数或类型。

示例:

// not exported function (private)
func hello() {
    fmt.Println("hello")
}

// exported function (public)
func World() {
    fmt.Println("world")
}

只能从其他程序包访问导出的函数和类型。

答案 1 :(得分:4)

Go中的规则是,以大写字母开头的结构,变量和结构成员为“ public”,以小写字母开头的结构,变量和结构成员为“ private”。

但是,这并不完全等同于私人/公共。它更像Java中的package-private。这意味着您可以从同一包中访问结构的“私有”成员,但是当您将结构导入其他包时,它们将被隐藏。

所以:

package foobar

// This is visible when your package is imported elsewhere.
type Foo struct {
  // This is also visible to other packages.
  ThisIsPublic string
  // This isn't.
  thisIsPrivate string
}


// This is visible only in the current package.
type foo struct {
}

// Exported
const PublicConst = 1
// Not exorted
const privateConst = 2

// As well as this
func ExportedFunc() {
}

// And this isn't...
func unexportedFunc() {
}

顺便说一句,这有一个相当时髦的(ab)用例,它允许包中的公共函数返回私有类型!所以这件事可行:

package pkg

type foo struct {
   Bar string
}

// This function is exported and allows the caller to get a foo.
func GetFoo() foo {
   return foo{Bar: "bar"}
}

------
package main

import "pkg"

func Wat() string {
   // f is of the private type foo.
   f := pkg.GetFoo()
   // but we can access its public members!
   return f.Bar
}

答案 2 :(得分:1)

Go不是一种功能语言;正确的方法具有OOP功能,您也只需考虑一下Go-way。

关于公共和私有访问的重要注意事项是,以小写字母开头的结构字段和函数是“私有字段”和“函数”,不能在声明它们的包之外访问。

用go的话来说,这意味着它们是“未导出”的字段或函数。

type privateStruct struct {
    Field int
}


func unexportedFunction() {

}

type ExportedStruct struct {
   Field int
}

func ExportedFunction() {

}

答案 3 :(得分:0)

go中没有这种封装,隐私不能以这种方式起作用,通常,如果要访问变量,函数或结构,则必须将其放在同一包中。

但是工程师还是会采用一些样式,如果函数或结构仅在同一包中使用,我们可以说它是“私有”的,则必须以小写字母开头该名称:

function thisIsPrivateMethod {
  // some instructions
}

type privateStruct struct {
  value string
}

反之亦然:

function ThisIsPrivateMethod {
  // some instructions
}

type PublicStruct struct {
  value string
}

答案 4 :(得分:-2)

如果使用OOP类型的编程。您可以使用以下示例:

package example

// func like static function in java
func privateFuncInPackage() string {
    // ... do something
    return "string"
}

func PublicFuncInPackage() {
    // ... do something
}

// like class
type privateClass struct {
    // any thing ...
}

// like protect function
func (p *privateClass) privateFuncOfPrivateClass() string {
    // Call private func of package
    return privateFuncInPackage()
}

type PublicClass struct {
    privateProperty string
    publicProperty  string
    // any thing ...

    // extend class
    *privateClass
}

// like protect function
func (p *PublicClass) privateFuncOfPublicClass() string {
    return p.privateFuncOfPrivateClass()
}

// like public function
func (p *PublicClass) PublicFuncOfPublicClassSetProperty(newP string) {
    p.publicProperty = newP
    // do something ...
}

type AnInterface interface {
    FuncOfInterface() string
}

// `PublicClass` implement interface `AnInterface`
func (p *PublicClass) FuncOfInterface() string {
    // do something ...
    return "Was impled"
}

func init() {
    // Create instance of class
    myPublicInstance := &PublicClass{}
    // Or
    myPublicInstance = new(PublicClass)
    _ = myPublicInstance
}

希望以上示例能够补充其他人的答案,并帮助您更多<3