Golang:my Introduction to the Go programming language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

I recently discovered Go, a new-ish programming language fostered at Google. I jumped right on and wrote a blog engine with it. My background is PHP. I ' m used to the fast and loose school of programming. Getting something out there that works and fixing it later. Using Go is a change for me. It is my first time working with a strongly typed language, a compiled language and something closer to C without a lot o F OOP stuff baked in.

The syntax of Go is very strict. One little character out of place and the compiler would let you know. At first I found myself stumbling through compiler warnings and errors. The syntax isn ' t too different than PHP or other C style languages. There is differences in the core theory of some types. Like the difference between an array and a slice. Where An array is statically sized, a slice are a dynamically sized abstraction built on top of an underlying array.

Jump inch

I threw myself into learning Go. I setup a DEV instance and just started coding. It wasn ' t pretty. In fact it is ugly code. But I am able to write code that does what I wanted it to do. I stumbled back and forth learning as I meandered through syntax and code patterns. The online documentation is really good, and help me understand the language and gave me good examples which to mimic.

Along with the many features of the standard library and tools Go have a Web server as part of the Net/http library. I was able the use of this server to serve Go generated content. It listened on a localhost port, and requests were proxied from the front end by Apache. A nice-to-integrate a Go application server with Apache running static content for existing websites.

Generating content

Go has own templating language. In text and HTML varieties. The templates is straightforward and easy-to-understand, however this is my greatest challenge. The Go templateing language contains very little in the the to logic statements or conditional testing. So no if (value = = value). Those kinds of decisions in the functions in your application and only send a final data struct To the template to be displayed. They has a Boolean "if" statement to check if something exist or doesn ' t. But there is no built-in-a-check a value or see if it was greater or smaller or make a decision on whether to display it.

For templates you can create filters, which is functions that a template can call the act in the current data structure. Instead of the template checking a usertype number to see if it's greater than the access needed for an action you would Create a filter that would check the Value-and then return a data value to the template.

I created a filter to check for " " (more) in the blog body and to cut off output in the template. This is used in pages with multiple posts where we want to only show a summary and not the whole blog post.

The Go to Things

The philosophy of Go is small, simple and direct. It's not quite the same as Python's "only one-to-do it" statement, but it's along those lines. Go wants to is it small and not clutter developers mind with too many things. They don ' t has a "do" or "while" loop. Instead They suggest the use of a "for" loop.

Go is opinionated. There is one-of-a-should format your code, and that there is a code formatting tool, which makes it easy-to-do. It ' s philosophy that we should write code correctly not just for today, but we should also is doing it correctly for Tomor Row. With PHP there be a ton of ways to format your source and every developer have a different opinion on? But it's doesn ' t matter with go. That's not a part of the conversation any more. Everyone formats code the same and moves on.

Go works for me. I can edit, compile, and test just about as fast as I could with PHP, and I get the added benefits of static types, direct Syntax and fast compiled code. It has some idioms and quirks that would take some time getting used to, but I was productive faster than I Expected-withou T has any background in C or C + +.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.