Embrace Go – A modern programming language

Let’s start with the Language Trend
in Github.com. There is no doubt, Java tops the chart amongst all compiled languages. Java 1.0 was released in 1995 and we haven’t had the need for another general-purpose programming language ever since.

Go, on the other hand recently announced
its latest release. If the success stories of few early adopters
are any indication, the language has a great future ahead.

You probably read and some of you probably tried the new language from Google, rightly named so – short and sweet. The language is getting popular
by the day and for good reasons. Obviously for the mass, it is not the language of choice yet. Having said that, there is a huge community and momentum
out there we cannot afford to ignore.

In this article, we’re going to go through everything you need to know in the Go ecosystem. If you like starting from “Hello World”, Start Here
, play with Playground
, read the FAQ
and Effective Go
.

Facts

  • Go offers a
    Playground

    for Developers to experiment and share code – gotta love that.
  • Go is
    Open Source

    and Small
    – The complete spec
    is less than 40 pages.
  • Go is a Compiled
    ( Statically-Typed
    ) language.
  • Go supports GC
    and Concurrency
    .
  • Go is Opinionated
    – leaves no room for ambiguity and interpretation. Checkout gofmt
    , golint
    and other productive commands
    .
  • Go is a Modern
    language – Designed for the web and teams. Supports networked and multi-core computing.
  • It is
    Simple

    and
    Fast

    – It has its origins
    in the C programming language.
  • Stay up-to-date with The Weekly Newsletter
    from Go.

Into the Weeds

In this section, we’ll deep-dive into some concepts that are important in the long run:

Zero Values

Zero values are the default values for various data types.

Function

Function is a first-class
citizen in Go. A function in Go can take varargs
, and can return multiple types. It can be anonymous
and use defer
to do things later.

Structure, Method and Receiver

Go is not Object-Oriented but gives you features that gets you close enough. You may have already guessed it by now. Yes, a Structure
in Go allows you to capture both data and behavior.

The functions associated to a Structure are called Methods
and the runtime structure object is called a Receiver
. A receiver can be a pointer or a value object.

Interface

Read this thrice. Yes, three times.

An Interface
in Go is used to define behavior ( Method
) and really any type
can implement that behavior implicitly
. There is no implements keyword. In other words a type can take multiple forms by simply associating Method
(same signature) from different Interfaces.

Pointer vs. Value

There is a subtle (but huge) difference between these two and it is important to understand that distinction. Think about a link (URL) to a web-page as a Pointer
and the content of the page as Value
.

Functions (or Methods) interact with each other by (a) passing type/data and (b) receiving type/data. Either way it can be a Pointer
(Pass by Reference) or a Value
(Pass by Value).

With a Pointer
you can check against


as well as change the original data .
In contrast with a Value
you cannot check against
and you cannot change the original data. So use Pointer
to mutate and Value
for immutability.

Let’s try the same example as before, but this time we’ll change the Receiver to a Value
as opposed to a Pointer
.

Goroutine and Channel

Goroutines are threads. Channels are a typed conduit through which you can send and receive values with the channel operator, <-. The direction of arrow determines the flow (to or from).

Vendoring

Vendoring in Go is what we know as dependency management. Go lets you use specific versions from third-party vendors go into a specific
vendor

folder. So here is how you can accomplish this:

  1. For Go 1.5 or less, export GO15VENDOREXPERIMENT=1
  2. Get the package you need using a dependency management tool

More here

Server Side

Server side programming has never been so easy. Yes, you can write a simple (or complicated) http server in Go with ease. No Web-Server or App-Server is needed.

Obviously you cannot run this example in Playground. So, save this example to a go file (say server.go) and run from command-line.

> go run /server.go

This will start the server at port 8080. Now try this
from your browser.

Dockerizing Go Programs

Again it is fairly simple. Use this Dockerfile and try the server we build earlier.

# Get image from https://hub.docker.com/_/golang/
 FROM golang:1.6
 # Setup Vendor (if needed)
 #ENV GO15VENDOREXPERIMENT=1
 # Copy your code to the image.
 COPY . /go/src/github.com/my-repo/server
 # Build & Install
 RUN cd /go/src && go install github.com/my-repo/server
 # Run the app
 ENTRYPOINT /go/bin/server
 # Expose port 8080.
 EXPOSE 8080

Next Steps and Resources

稿源:Washington Developer Blog (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合技术 » Embrace Go – A modern programming language

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录