Using GO after used to PHP

Awang Trisakti
3 min readFeb 13, 2023

--

Photo by Ben Griffiths on Unsplash

Hello everyone, my big apologize after vacuum writing post in medium due some excuse. In this post I just want sharing about my experience trying Go programming language after I used to php. Some time ago I moved into a new work place with a new project that use Go as its stack.

For those who don’t know, Go is a statically typed, compiled high-level programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. It is syntactically similar to C, but with memory safety, garbage collection, structural typing, and CSP-style concurrency. It is often referred to as Golang because of its former domain name, golang.org, but its proper name is Go.

There is some what I learned from this stack transition that I want to share in this post.

Dynamic vs Static
PHP is dynamic type which means that by default there is no need to specify the type of a variable, as this will be determined at runtime.

// This variable assigned as string
$a = "strings";

// Now this variable is int
$a = 1;

Go is static type which means that you can’t change variable’s type at runtime.

// This variabel assigned as string
var a = "string"

// This will caused error
a = 1

Error handling
Go has different error handling compared to several programming language such as php, javascript, java and other programming language that use try catch block to handle the errors or exception. Handling error in Go is to compare the returned error to nil, a nil value indicates that no error has occurred and not nil error indicates the presence of an error.

func Divided(a int, b int) (int, error) {
if b == 0 {
return 0, fmt.Errorf("can't divide %d by zero", a)
}
}

divided, err := Divided(10, 0)
if err != nil {
log.Fatal("error occurred", err)
}

Object oriented
PHP provide all features of OOP like class, object, inheritance, polymorphism, abstraction while Go doesn’t provide classes to use but we still can use struct like an object in OOP which methods can be added into the struct.

// Cat struct
type Cat struct {
Name string
}

// Add method walk to cat struct
func (c *Cat) Walk() {
fmt.Printf("%s is walking", c.Name)
}

// Call method walk
var cat Cat
cat.Name = "Puss"
cat.Walk()
// output
// Puss is walking

In other, Go also has interface which it contains methods definition similar to interface in OOP. Implementation interface in Go is implicit by implement all its methods. There are no explicit declaration.

// Animal interface
type AnimalInterface interface {
Walk()
}

// Cat struct
type Cat struct {
Name string
}

// Implement method walk to cat struct
func (c *Cat) Walk() {
fmt.Printf("%s is walking\n", c.Name)
}

// Duck struct
type Duck struct {
Name string
}

// Implement method walk to duck struct
func (d *Duck) Walk() {
fmt.Printf("%s is walking\n", d.Name)
}

// Func to run all animal action
func AnimalAction(animal AnimalInterface) {
animal.Walk()
}

// Call function animal action
var cat Cat
cat.Name = "Puss"
var duck Duck
duck.Name = "Donald"

AnimalAction(&cat)
AnimalAction(&duck)
// output
// Puss is walking
// Donald is walking

Conclusion
I think this transition is a bit of confusion but I don’t know why I’m so exciting to learn more deeply about Go.

Thats all that I can share, thanks for read my post. If you have any question or suggestion please feel free to leave comments below. Share with others if you found this post useful.

See ya

References
-
https://go.dev/
- https://en.wikipedia.org/wiki/Go_(programming_language)
- https://www.php.net/

--

--

No responses yet