[Go] 浅谈 Golang struct 与 PHP class 的相似

通过llama.cpp与羊驼聊天的网页界面- 详解 Serge 的启动使用

 

Golang 中的 struct 与 PHP 的 class 在使用方式上差不多。

 

struct 中的成员可以类比 class 中的属性,struct 中的成员函数可以类比 class 中的方法。

 

对比示例:

// Golang

type CollectorOption func(*Collector)

type Collector struct {
    UserAgent string
    MaxDepth int
}

func NewCollector(options ...CollectorOption) *Collector {

}
func (c
*Collector) Visit(URL string) error { }

 

// PHP

$options = function (&instance) {
}

function NewCollector(...$options) Collector {
}

class Collector {
  public $userAgent;
  public $maxDepth;

  public function visit(string $url): string
  {
    //
  }
}

 

Link:https://www.cnblogs.com/farwish/p/12620239.html

posted on 2020-04-02 15:46  ercom  阅读(532)  评论(0编辑  收藏  举报