Programming Notes ✍️ – Telegram
Programming Notes ✍️
12 subscribers
65 photos
1 file
22 links
Download Telegram
new() is the only way to get a pointer to an unnamed integer or other basic type. You can write "p := new(int)" but you can't write "p := &int{0}". Other than that, it's a matter of preference.
What's the difference between '&' and 'new' on Golang? “ new ” creates an instance of an object in the heap and returns a pointer, “ & ” dereferences an object returning a pointer. Go allows escaping pointers (unlike C).
// Singleton implementation
type Singleton interface {
Add() int
}

type DB struct {
count int
}

func (s *DB) Add() int {
s.count++
return s.count
}

var Pointer *DB

func GetInstance() Singleton {
if Pointer == nil {
Pointer = new(DB)
}
return Pointer
}

func main() {
var single = GetInstance()
single.Add()
fmt.Println(single.Add())

fmt.Println(single)
}
To enable communication between goroutines, a simple approach is to use a locked queue (a “First-In-First-Out” data structure). Senders add items to the queue, and receivers take items from it. Interestingly, Go uses a similar concept for inter-goroutine communication, and they’ve named it “channels.”
Protocol Buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data.
It’s like JSON, except it’s smaller and faster, and it generates native language bindings. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.

Protocol buffers are a combination of the definition language (created in .proto files), the code that the proto compiler generates to interface with data, language-specific runtime libraries, the serialization format for data that is written to a file (or sent across a network connection), and the serialized data.
OOP focuses on modeling programs around objects, which possess both data and methods.

FP concentrates on treating data as mathematical functions without altering their state.