Introduction to Go for PHP Developers

Introduction to Go for PHP Developers

Introduction to Go for PHP Developers - There are some notable differences with PHP though, so I want to share them for any other PHP developer who wants to learn Go in the future.

Originally published by Andrew Davis at dev.to

Recently, I started working on an internal CLI app for my team at work. My main programming language of choice is PHP, but I wanted to create this program in a language that could run on any platform without having to have an interpreter already installed. I also wanted the app to be self contained in a single binary for easy distribution and installation. I downloaded Go and was pleasantly surprised at how easy to learn the language is and how productive I got with it in a short amount of time. Go’s procedural programming model really clicked with my PHP brain and I was able to get this app up and running quickly. There are some notable differences with PHP though, so I want to share them for any other PHP developer who wants to learn Go in the future.

Installation

MacOS

I use a Mac so I installed Go using Homebrew: brew install go. If you don’t have Homebrew on your Mac, I highly recommend using it.

Windows & Linux

The Go website has downloads for each OS, including a walk-through installer for Windows that makes the installation easy.

Getting Started

In Go, all source code for each Go project is stored under a single directory called the GOPATH. By default, the GOPATH is set to go in your home folder, e.g. /Users/andrewdavis/go. In the GOPATH, there is a bin directory and a src directory. The bin directory holds any binaries you download as dependencies. You will want to add the bin folder to your PATH environment variable. You can do so in your terminal’s .bashrc/.zshrc file with export PATH=$PATH:$(go env GOPATH)/bin. To start learning Go, you can download the Tour of Go program by running the following in your terminal go get golang.org/x/tour/gotour. go get downloads the source code and binary for a third party dependency using the provided path. Now, you can run gotour in your terminal and it will start a web server and point your browser to it.

To create a project, make a directory under src in your GOPATH: mkdir -p $GOPATH/src/helloworld. Open that folder cd $GOPATH/src/helloworld and create a file called main, touch main.go. In that file, put the following:

package main

import "fmt"

func main() {
fmt.Println("Hello world!")
}

The starting point for all Go programs is the main function in the main package. Next, you can run go run main.go to run the program. You can also run go install and the program will be compiled and put in the bindirectory so you can execute helloworld in your terminal and it will run your code.

Major Differences from PHP

Now that you have a project set up, you can start exploring the different Go features. One of the first things you will notice is that semi-colons are not required in Go. The end of a statement is detected by a new line. Here are some more differences that took me some time to understand:

Variables

Go is a statically and strongly typed language so every variable has a type assigned to it. Variables in functions are assigned using the := operator and this operator will automatically set the variable type for you: name := "Andrew" // name is now a string. To create a variable without setting any data in it or to create one outside of a function you have to use the var keyword: var name string.

If statements

If statements work the same as they do in PHP, however they don’t have to use parentheses around the boolean check. The difference confused me at first when reading Go code. However, I think it makes the code a little easier to read.

package main

import "fmt"

func main() {
value := false
if value {
fmt.Println("Value is true")
} else {
fmt.Println("Value is false")
}
}

Packages vs Namespaces

Go uses the term package to namespace it’s content. If you have a folder called controllers in your Go code, each file in that folder would start with package controllers. To import controllers from another package, you would write import “helloworld/controllers”. Anything in your package that has a name that starts with a capital letter can be used in another package. If you have a function named, func HelloWorld() in controllers, you would then be able to call controllers.HelloWorld() to run the function once controllers is imported. Anything that does not start with a capital letter can only be used from the same package. No need for private or public!

Strings

In Go, all strings must be surrounded by double quotes. A single quoted value in Go is for a rune (a single Unicode code point). By habit, I type my strings with single quotes since that is common practice in PHP. It takes a little time to adjust to always using double quotes.

var name = "Andrew"
var copy = '©'

Structs vs Classes

Go does not have a class system like PHP. Instead, it uses structs to model custom data structures. You can write a struct like this:

package main

type Cup struct {
name string
color string
volume int
}

You can add methods to a struct by creating a function that references a struct in parentheses before the function name.

func (c Cup) nameAndColor() string {
return c.name + ": " + c.color
}

You can then create an instance of a struct by writing the struct name and passing in its initial values in curly braces. Method execution uses dot notation.

func main() {
c := Cup{name: "Solo", color: "Red", volume: 12}
c.nameAndColor() // returns "Solo: Red"
}

To create a method that modifies the struct instance, the method must reference a pointer to the struct:

func (c *Cup) crush() {
c.volume = 0
}

Errors

In Go, errors are not treated like exceptions. There is no throw or catch mechanism. Instead, errors are returned from functions if one has occurred. Go supports returning multiple values from a function. If you call a function that could return an error, you have to check if the error is not nil to handle the error condition.

package main

import "fmt"

func GetName(name string) (string, error) {
if name == "Bob" {
return "", fmt.Errorf("Name cannot be Bob")
}

return name, nil
}

func main() {
name, err := GetName("Bob")
if err != nil {
fmt.Println("Uh-oh an error has occurred")
}
}

Fin

Of course, there is a lot more to learn about Go, but hopefully this will help you get started. There are a lot of great resources for studying Go. The most helpful for me were the Go docs and Go By Example. If you have any thoughts or questions, please leave a comment. Thanks for reading!

The PHP logo comes from the PHP website.

The Go gopher image comes from @egonelbre.


Originally published by Andrew Davis at dev.to

===================================================

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

☞ Learn How To Code: Google’s Go (golang) Programming Language

☞ Go: The Complete Developer’s Guide (Golang)

☞ Web Development w/ Google’s Go (golang) Programming Language

☞ Go: The Complete Bootcamp Course (Golang)

☞ Mastering Hyperledger Chaincode Development using GoLang

☞ A gentle introduction to Google Go (Golang) for Beginners

☞ PHP for Beginners - Become a PHP Master - CMS Project

☞ Object Oriented PHP & MVC

☞ PHP OOP: Object Oriented Programming for beginners + Project

☞ Learn PHP Fundamentals From Scratch

☞ The Complete PHP MySQL Professional Course with 5 Projects

Go Top Programming Languages in 2020 from Authentic Surveys

Go Top Programming Languages in 2020 from Authentic Surveys

This article going to present the trends of top Programming Languages which will continue in the coming year 2020. This article uses data from authentic surveys, various collected statistics, search results and salary trends according to programming languages.

Comparing Programming Languages is a very complex thing and so there are many graphical illustration/jokes trying to symbolize Programming language. I found few and I am starting this article with those.

In simple words, Programming Language empowers human to instruct and control machine. So, it is natural that there will be so many languages which try to make this process more powerful and simple. For this very reason there are hundreds of programming languages, many of those programming languages are now out of active use, few are going to be obsolete in coming years and then there are languages which is going to continue and prove its usage in coming years and then there are new programming language fighting for it acceptance.

This article going to present the trends of top Programming Languages which will continue in the coming year 2020. To predict the trend of the programming language in 2020 this article uses data from authentic surveys, various collected statistics, search results and salary trends according to programming languages. This article will help the new learner to pick a programming language to learn and for expert, it will help to decide either to switch to another language or to continue with his expertise language.

In the next section, I have prepared two tables which summarize the popularity trend of Programming Languages in the last five years (2015-19). The data is taken from Stackoverflow popularity survey 2015-2019. For a clear and accurate understanding, the programming languages are divided into two groups, first, languages which have origin before 2000 and the second group has languages which came after 2000. The selection of 2000 as the boundary is just random but very helpful to understand the programming trend under these two groups. The table also lists origin year and main or documented purpose of these programming/scripting languages.


Observations:

There is a decrease in the popularity of all languages from 2018 to 2019 except Python.

Python

Python is the only language continuously on rising since last five years. It is a general-purpose language, so someone wants to learn just one programming in 2020 and want to cover more area of software development then Python could be chosen**.**

Java

Java was on rising but fall in 2019, the reason could Kotlin gaining popularity on the Android platform. Java is a good choice for a programming language but now it is under Oracle and Google is promoting Kotlin so it is in the conflicted zone. As a matter of fact still, the large number of the company is using Java and going to continue with Java due to its developers base, framework, and legacy application.

C/C++

C and C++ are still holding with approx 20% and it will be there due to its inherent features and legacy system.

JavaScript

JavaScript popularity can be attributed to the growth of popular JavaScript library and framework like node.js, etc. JS is the language for the dynamic website and this going to be top for coming years because of its active development, support from Mozilla and penalty of libraries and frameworks. So, if someone wants to be web development, javascript is a must.

R

R is gaining popularity in recent years and reason would be growth and popularity of data analysis. It is used by data scientist but again much behind in comparison to Python which has established as general-purpose languages and enjoy active developers with lots of data science libraries and modules. So, one can prefer Python over R if they have to choose only one otherwise if wanted carrier in Data Sciences then learning both will a good option.

Ruby

Like PHP, Ruby is also facing tough competition from JavaScript and even Python to establish as back-end web development programming language. So, again for web development javascript and Python (server-side (Flask, Django, etc.) would be a good choice and will offer more domain flexibility than Ruby.

PHP

There is a sharp decline in PHP popularity in 2019 and it can be traced back to server-side acceptance of javascript and Python. So, if someone wants to go to server-side web development then still PHP is a good choice with a large number of popular framework like CakePHP, Codeigniter, etc. otherwise choosing a general-purpose programming language would be better.

Objective-C

Objective-C was the main language for Apple’s software such as macOS, iOS, etc. before Apple moved to Swift language. So this transition is reflected in the popularity of both languages, i.e. there is a fall in popularity for Objective-C and the popularity of Swift is rising. So, again if someone wants to be a developer for Apple’s products then Swift should be the language of choice.

Observations:

Swift

Swift has replaced the Objective-C as the main language for Apple-related software and application. Since it is supported and promoted by Apple so there is an increase in popularity since its inception and as Apple is going to continue with it so if someone is looking for Apple-specific development platform then Swift is going to be a must-know programming language. This is mostly vendor and product-specific language with very less usage outside Apple’s eco-system.

Go

Go (Golang) is getting popularity as maintain, use and promoted by Google. The motivation of Go development was to address criticism of some of the popular languages and keeping the best of them in one place. Since 2017, Go is moving upward in popularity and with Google support, it is going to enjoy this in coming years. Google is also making Go as a primary language for new projects and replacing other languages with Go, this trend going to make useful and important to learn in coming years so one can pick Go as a new programming language.

Kotlin

Kotlin is being offered as an alternative of Java for Android development and again it is supported and promoted by Google so it is also picking up by developers and gaining popularity in recent years. So, with the growth of Android, support of Google and with clean and short syntax it is going to be a choice of Android application developers and is a good choice to learn for Android App developer. Kotlin going to be shine as a prominent programming environment for Android development.

Scala

Scala tries to establish as an alternative to Java but didn’t get very well among developers. It doesn’t have big support from any multi-national company, perceive as functional languages and dependency on JVM doesn’t provide much scope to rise in popularity. There could be steady growth but very slow and surely not a language to learn as a beginner.

Julia

Julia aims to bring the speed of ‘C’ and simplicity of Python but it is strange that didn’t found any popularity in Stackoverflow survey but gaining popularity among data science domain and being seen as a challenger for R and Python in long run. Surely, there will be growth in Julia but still, Python or R is better for job and growth.

C#

C# is the language for the .NET framework and developed by Microsoft. Its popularity is approx constant over past years and going to continue with a similar trend. This is vendor-specific language so one can pick this language if want to work in the Microsoft development environment. Recently, Microsoft has open-sourced the .NET so there would be some upward trend but again it is vendor-specific so there won’t be much affected.

Rust

Rust, Clojure, etc. are languages which have a user-base but not so popular so surely not going to have an upward swing in popularity.


A Picture Says a Thousand Words

To understand a clear trend and picture of top programming language growth let keep a picture of it by the various chart. The figure 1 and figure2 gives a very clear picture that in old language stack JavaScript is far ahead than others and credit goes to boom in web development, then C and C++ together competing very closer to Java. Python is moving upward in popularity and only language which popularity is constantly increasing in the last 5 years. New languages are getting popularity and most of them are supported by the multi-national company and bit IT giant like Microsoft, Google and Apple.

Loved and Wanted Languages

From above Table and Figure, few observations are very obvious, Love of Rust is growing in last five years whereas Swift is loosing love from developers and Python is in between these two and last two years have gain for Python. One more unique observation is that out of 5 loved languages 4 are from post 2000 group while only Python is the older language and Kotlin love started with addition of Kotlin for Android development post 2017.

From above table, wish of developing in javascript and Python is growing in last years and this reflect in popularity and love for the language. There is a sharp decline in Java and this is due to the addition of Kotlin as alternative for Android app development and also change of policy by Oracle who own Java now.

Technologies and Programming Languages

In this figure, one can see that the largest cluster is for Web development and JavaScript and its various framework is dominating the cluster this is USP of JavaScript growth. The second-largest cluster is of Microsoft technologies and Python technologies which again clear the popularity and love for the language. Python cluster is linked with data science technologies which highlight the growth story of Python.

TIOBE Index

TIOBE index rank programming language based on search engine search result. The selection of search engines and programming language is defined in its page. The ratings are calculated by counting hits of the most popular search engines. The search query that is used is +” programming”. In TIOBE index Java is dominating the ranking in the last two decades where C is holding the 1st and 2nd rank for the last 30 years. Python has come a long way in the last two decades i.e. 24th in 1999 to 3rd in 2019. If someone merges the C and C++ then it would hold the 1st positions forever.

In the new languages (post-2000), Rust moved up in ranking i.e. from 33rd to 28th, and Julia from 50th to 39th. It is also interesting to note that Kotlin doesn’t seem to come closer to the top 20.

Popularity of Programming Language (PYPL) Index

The PYPL index is created by analyzing how often language tutorials are searched on Google. The more a language tutorial is searched, the more popular the language is assumed to be. It is a leading indicator. The raw data comes from Google Trends.

Below Figure verifies that the top 3 languages are Python, Java, and JavaScript. C#, PHP, C/C++ also secure top position, this trend is again similar to stack-overflow, and TIOBE index.

Above Figure indicates that among new programming Language i.e. post 2000 Kotlin, Go, Rust, and Julia is moving up in the ranking.

Job Market and Salary

Salary depends upon the geographical area and demand of the products, a programming language based salary comparison is just a tool to predict or estimate the salary trend. We have summarized salary based on programming language from popular survey i.e. Dice salary survey 2018 and Stack-overflow survey 2018 and 2019.

From the above table, it is very clear from both survey that Go/Golang is a very high paid job in the market and even stands 1st rank in a high paid job in stack-overflow 2019 survey and Dice Salary Survey 2018.

Language Predictability

So, as closing remarks, It is easy to predict a language trend but choosing only one language to learn is a really difficult choice and totally depend upon the individual choice and their future plans, for example, if you want to work in Web Development you can’t afford neglecting Javascript, if you want to work with Apple’s products you can’t neglect Swift now, if your taste is in system-level programming then C and C++ is your friend, Python makes you run faster in many domains and currently darling in Data science. You see each language takes you on a different journey. Choose your destination and then drive with the language of that path.

You may also like: Programming Languages - Trend Predictions in 2020 and Next Years.

We’ll love to know your opinion. What is the Best Programming Language for you?

Thank for reading! If you enjoyed this article, please share it with others who may enjoy it as well.!