Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
Go не любит late binding, но любит messaging, а концепция «свойств и поведения» объектов реализована великолепно, и это дает повод называть Go великолепным ОО-языком.
Интерфейсы и вызов методов конкретных типов через интерфейсы и есть late binding.
func (s TwitterSource) Find(word string)
func (s *TwitterSource) Find(word string)
func (s string) Find(word string) ([]Message, error) {...}
type Plaintext string
func (s Plaintext) Find(word string) ([]Message, error) {...}
func (s Plaintext) Find(word string) ([]Message, error) {...}
type AliasOfT T
func (s T) MethodABCD() { .../* надо вызвать это */ ...}
func (s AliasOfT) MethodABCD() { super.MethodABCD() }
func (s AliasOfT) MethodABCD() { T(s).MethodABCD() }
В Go используется правило регистра первой буквы имени — если название начинается заглавной буквы — это public-доступ, если со строчной — private. Вначале может показаться посягательством на свободу слова неудобством, но с первых строк на Go, понимаешь, насколько это было удобное решение.
func foo()
{...
}
Я лично пока-что не понимаю, какой есть use-case, когда необходим generics.
func Sort(func compare(a,b T) bool, []T) []T
func Filter(func fine(a T) bool, []T) []T
func Map(func apply(a T) Ta, []T) []Ta
func Fold(func combine(a, b T) Tc, []T) Tc
Люди, как правило, просто хотят решать задачу тем же путем, которым они привыкли в прошлых языках.
stdlib sort использует reflection, что медленно и велосипед
godoc.org/bitbucket.org/santucco/btree построен на encoding/binary, то есть не typesafe, а скорее untyped
//имея
func Btree(node T) Btree T {...}
//написать
var bt Btree int
//так же легко как
var sl [] int
//
//и typesafe
type T; //type parameter
func twotime([]T) []T {...}
//или
func twotime(bt Btree T) bt Btree T {...}
//вместо untyped
func twotime(sl []interface{}) []interface{} {...}
To take one example, Hindley-Milner type inference is very well understood and I think generally accepted in the programming language community as a good feature. But new ML programmers inevitably hit the situation where changing one part of a program causes a mysterious type error in a seemingly unrelated part. The solution is that in practice one writes type signatures for most functions. Hindley-Milner type inference is beautiful research, but it’s not as beautiful in practice. In Go, the only type inference is that if you say var x = e, the type of x comes from e. This rule is simple and predictable and doesn’t suffer from the kinds of “spooky action at a distance” of more complex type inference algorithms. I miss having more complex inference sometimes, but Go’s simple rule handles at least 90% of the cases you care about, with none of the unpredictability or mystery.
Но расстановка семиколонок и навязанный стандарт gofmt — для меня слишком proprietary and opinionated.
gofmt
— одна из самых замечательных вещей в Go. Никаких тебе больше code style guides. Никаких споров о tabs vs spaces и о размерах отступов. Не надо больше думать, переносить фигурную скобку на следующую строчку или нет.
Golang и ООП