golang: My introduction to the Go programming language

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

I recently discovered Go, a new-ish programming language fostered at Google. I jumped right in 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 was a change for me. It was my first time working with a strongly typed language, a compiled language and something closer to C without a lot of OOP stuff baked in.

The syntax of Go is very strict. One little character out of place and the compiler will 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 are 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 is a dynamically sized abstraction built on top of an underlying array.

Jump in

I threw myself into learning Go. I setup a DEV instance and just started coding. It wasn't pretty. In fact it was ugly code. But I was able to write code that did 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 has a web server as part of the net/http library. I was able to use this server to serve Go generated content. It listened on a localhost port, and requests were proxied from the front end by Apache. This is a nice way to integrate a Go application server with Apache running static content for existing websites.

Generating content

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

For templates you can create filters, which are functions that a template can call that act on the current data structure. Instead of the template checking a userType number to see if it is 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 on pages with multiple posts where we want to only show a summary and not the whole blog post.

The Go way of things

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

Go is opinionated. There is one way you should format your code, and that there is a code formatting tool that makes it easy to do so. It's philosophy that we should write code correctly not just for today, but we should also be doing it correctly for tomorrow. With PHP there are a ton of ways to format your source and every developer has a different opinion on what that is. But it doesn't matter with go. That's not part of the conversation any more. Everyone formats code the same way 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 will take some time getting used to, but I was productive faster than I expected—without having any background in C or C++.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.