2015-09-13
Composition goes beyond the mechanics of type embedding. It’s a paradigm we can leverage to design better APIs and to build larger programs from smaller parts. It all starts from the declaration and implementation of types that have a single purpose. Programs that are architected with composition in mind have a better chance to grow and adapt to changing needs. They are also much easier to read and reason about.
2015-09-08

Note: This article extends Dave Cheney’s Go 1.5 cross compilers post.

Cross compilers got easier with Go 1.5. You don’t have to bootstrap the standard library and toolchain as you used to do prior to 1.5.

If cgo is not required

The go tool won’t require any bootstrapping if cgo is not required. That allows you to target the following program to any GOOS/GOARCH without requiring you to do any additional work. Invoke go build.

$ cat main.go
package main
import "fmt"
func main() {
    fmt.Println("hello world")
}

In order to target android/arm, run the following command.

$ GOOS=android GOARCH=arm GOARM=7 go build .

The produced binary is targeting ARMv7 processors that runs Android. All possible GOOS and GOARCH values are listed on the environment docs.

If cgo is required

If you need to have cgo enabled, the go tool allows you to provide custom C and C++ compilers via CC and CXX environment variables.

$ CGO_ENABLED=1 CC=android-armeabi-gcc CXX=android-armeabi-g++ \
    GOOS=android GOARCH=arm GOARM=7 go build .

The toolchain will invoke android-armeabi-gcc and android-armeabi-g++ if it is required to compile any part of the package with a C or C++ compiler. Consider the following program with a slightly different main function. Rather than outputting “hello world” to the standard I/O, it will use Android system libraries to write “hello world” to the system log.

$ cat main.go
// +build android

package main

// #cgo LDFLAGS: -llog
//
// #include <android/log.h>
//
// void hello() {
//   __android_log_print(
//     ANDROID_LOG_INFO, "MyProgram", "hello world");
// }
import "C"
func main() {
    C.hello()
}

If you build the program with the command above and examine the build with -x, you can observe that cgo is delegating the C compilation to arm-linux-androideabi-gcc.

$ CGO_ENABLED=1 \
CC=arm-linux-androideabi-gcc \
CXX=arm-linux-androideabi-g++ \
GOOS=android GOARCH=arm GOARM=7 go build -x .
...
CGO_LDFLAGS=”-g” “-O2” “-llog” /Users/jbd/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/github.com/rakyll/hello/_obj/ -importpath github.com/rakyll/hello — -I $WORK/github.com/rakyll/hello/_obj/ main.go
arm-linux-androideabi-gcc -I . -fPIC -marm -pthread -fmessage-length=0 -print-libgcc-file-name
arm-linux-androideabi-gcc -I . -fPIC -marm -pthread -fmessage-length=0 -I $WORK/github.com/rakyll/hello/_obj/ -g -O2 -o $WORK/github.com/rakyll/hello/_obj/_cgo_main.o -c $WORK/github.com/rakyll/hello/_obj/_cgo_main.c
...

Pre-building the standard library

The go tool also provides a utility if you would like to pre-build the standard library, targeting a specific GOOS and GOARCH.

$ CGO_ENABLED=1 \
    CC=arm-linux-androideabi-gcc \
    CXX=arm-linux-androideabi-g++ \
    GOOS=android GOARCH=arm GOARM=7 go install std

The standard library targeting android/armv7 will be available at $GOROOT/pkg/android_arm.

$ ls $GOROOT/pkg/android_arm
archive    fmt.a      math       runtime.a
bufio.a    go         math.a     sort.a
bytes.a    hash       mime       strconv.a
compress   hash.a     mime.a     strings.a
container  html       net        sync
crypto     html.a     net.a      sync.a
crypto.a   image      os         syscall.a
database   image.a    os.a       testing
debug      index      path       testing.a
encoding   internal   path.a     text
encoding.a io         reflect.a  time.a
errors.a   io.a       regexp     unicode
expvar.a   log        regexp.a   unicode.a
flag.a     log.a      runtime

If you prefer not to pre-build and install the standard library to the GOROOT, required libraries will be built while building user packages. But, the standard libraries builds are not preserved for future use at this stage and they will be rebuilt each time you run go build.

2015-09-08
Note: This article extends Dave Cheney’s Go 1.5 cross compilers post. Cross compilers got easier with Go 1.5. You don’t have to bootstrap the standard library and toolchain as you used to do prior to 1.5. If cgo is not required The go tool won’t require any bootstrapping if cgo is not required. That allows you to target the following program to any GOOS/GOARCH without requiring you to do any additional work.
2015-09-05

Web technologies are in the heart of the software industry in this day and age. And one of the most popular approaches for web services to communicate with the outside world is the HTTP RESTful API design, which is integral in making modern software scalable and open. At the same time,  NOSQL databases are gaining more and more market share as … Continue reading "how to write a URL Shortener Golang Web Service with Mongodb?"

The post how to write a URL Shortener Golang Web Service with Mongodb? appeared first on Mina Andrawos.

2015-03-17

A friend of mine who is a very talented writer recently became intrigued with open source and asked me to help her to understand how to be a good open source community member.

Open source is one of the most unusual things in the world. Is there any other profession where highly skilled professionals donate their free time to give their work away for free? Many spend long hours at their day jobs, just to spend their nights and weekends doing the same thing.

I’ve been involved in open source for nearly 20 years – I’ve started some of my own successful community based projects (Hugo, spf13-vim, Cobra, Viper, Zoop), contributed to many others and been responsible for leading some of the most popular commercial open source projects in history (Docker & MongoDB).

Over the years I’ve learned 3 rules that, when followed, are sure to make you a good open source citizen.

Rule 1. Assume good will

Not only is this a good rule for open source, but it’s also a good rule for life. A lot of open source communication is done online in written formats. Community members come from all over the world and from very different cultures, so while most open source work is done in English, it’s often not a community member’s first language. Far too often I’ve seen these differences can lead to barriers and discord in the community when people assume that others are operating with malicious intent. While it’s true that there are some mean people out there, I have found that it’s always best to assume that people mean well until they prove otherwise.

Rule 2. Attitude of appreciation

Open source is built on on the backs of willing volunteers. Many people want to help out and contribute their time and talents to the best of their abilities. Far too often maintainers of projects have the attitude of “you’re not good enough”. I’ve even seen projects reject great contributions because of a spelling mistake in a comment. If you want to foster a healthy community (or participate in one), you need to cultivate an attitude of appreciation. This attitude manifests in different ways.

With all of my open source projects, as at the Docker project, we’ve adopted a policy of never saying “no”, but instead using contributor errors as opportunities to teach our community members how to make it a “yes”. Sometimes this works by showing them how to write better code, tests, or documentation. Sometimes it’s teaching them a valuable skill. This policy is generally helpful for the project overall, as once someone is taught they often have a desire to return the favor and pay it forward by helping other, less experienced members.

Another way appreciation is manifested is by accepting contributions even when they aren’t quite right. Oftentimes someone has taken a lot of time to contribute to a project, perhaps your project, and they didn’t get absolutely everything right along the way. Perhaps the documentation isn’t quite up to snuff, or the test coverage could be better. Remember that not everyone comes from the same background – if English isn’t their first language they may be embarrassed or unable to produce the quality documentation you need. In these cases, I’ve found it’s best to appreciate what they’ve done, take what they contributed, and carry it the rest of the way for them. Some people worry that contributors will be offended if you add to their contribution, but in my experience they’ve always been very appreciative.

Lastly, one of the most important ways to show an attitude of appreciation is to be responsive and thoughtful to questions, and especially contributions, that people provide. If you aren’t able to do this yourself, perhaps it’s time to ask for help. You’ll find great community members who are happy to support.

Rule 3. Don’t be afraid to jump in… ask questions, contribute.

Open source is made up of volunteers. Volunteers who don’t wait for an invitation to jump in and try to contribute. One of the best things you can do is ask “what can I do to help?”. I promise that most open source communities will not only provide many opportunities, but also the support you need to be successful.

Many people are afraid that their work will be rejected if they submit it, and this fear prevents them from contributing. I guarantee that two things will happen as you contribute to open source: 1) some of your work will be rejected, and 2) you will learn far more from these rejections than any other way. I speak not only from personal experience, but from conversations with many friends who are active contributors. To ensure that your contribution has the best chance of being accepted, discuss it with the project maintainers beforehand, during and after. They will be very open to helping.

2015-03-15

Go is an object oriented programming language. It may not have inheritance, but in this 20 minute video from the Bangalore meetup, I will show you how object oriented programming practices and techniques can be applied to your Go programs. From an object oriented standpoint, Go does provides the ability to add behavior to your types via methods, allows you to implement polymorphic behavior via interfaces and gives you a way to extend the state and behavior of any existing type via type embedding. Go also provides a form of encapsulation that allows your types, including their fields and methods, to be visible or invisible. Everything you need to write object oriented programs is available in Go.



Example 1 - Methods:
http://play.golang.org/p/hfRkC6nKag

Example 2 - Interfaces:
http://play.golang.org/p/F1UyKlTh3k

Example 3 - Extending Types:
http://play.golang.org/p/JJ811lBwoz

Example 4 - Overriding Inner Types:
http://play.golang.org/p/-xQFBv9_82

2015-03-15

Go is an object oriented programming language. It may not have inheritance, but in this 20 minute video from the Bangalore meetup, I will show you how object oriented programming practices and techniques can be applied to your Go programs. From an object oriented standpoint, Go does provides the ability to add behavior to your types via methods, allows you to implement polymorphic behavior via interfaces and gives you a way to extend the state and behavior of any existing type via type embedding. Go also provides a form of encapsulation that allows your types, including their fields and methods, to be visible or invisible. Everything you need to write object oriented programs is available in Go.



Example 1 - Methods:
http://play.golang.org/p/hfRkC6nKag

Example 2 - Interfaces:
http://play.golang.org/p/F1UyKlTh3k

Example 3 - Extending Types:
http://play.golang.org/p/JJ811lBwoz

Example 4 - Overriding Inner Types:
http://play.golang.org/p/-xQFBv9_82

2015-03-15

Go is an object oriented programming language. It may not have inheritance, but in this 20 minute video from the Bangalore meetup, I will show you how object oriented programming practices and techniques can be applied to your Go programs. From an object oriented standpoint, Go does provides the ability to add behavior to your types via methods, allows you to implement polymorphic behavior via interfaces and gives you a way to extend the state and behavior of any existing type via type embedding. Go also provides a form of encapsulation that allows your types, including their fields and methods, to be visible or invisible. Everything you need to write object oriented programs is available in Go.



Example 1 - Methods:
http://play.golang.org/p/hfRkC6nKag

Example 2 - Interfaces:
http://play.golang.org/p/F1UyKlTh3k

Example 3 - Extending Types:
http://play.golang.org/p/JJ811lBwoz

Example 4 - Overriding Inner Types:
http://play.golang.org/p/-xQFBv9_82

2015-03-15

Go is an object oriented programming language. It may not have inheritance, but in this 20 minute video from the Bangalore meetup, I will show you how object oriented programming practices and techniques can be applied to your Go programs. From an object oriented standpoint, Go does provides the ability to add behavior to your types via methods, allows you to implement polymorphic behavior via interfaces and gives you a way to extend the state and behavior of any existing type via type embedding. Go also provides a form of encapsulation that allows your types, including their fields and methods, to be visible or invisible. Everything you need to write object oriented programs is available in Go.



Example 1 - Methods:
http://play.golang.org/p/hfRkC6nKag

Example 2 - Interfaces:
http://play.golang.org/p/F1UyKlTh3k

Example 3 - Extending Types:
http://play.golang.org/p/JJ811lBwoz

Example 4 - Overriding Inner Types:
http://play.golang.org/p/-xQFBv9_82

2015-03-15
Go is an object oriented programming language. It may not have inheritance, but in this 20 minute video from the Bangalore meetup, I will show you how object oriented programming practices and techniques can be applied to your Go programs. From an object oriented standpoint, Go does provides the ability to add behavior to your types via methods, allows you to implement polymorphic behavior via interfaces and gives you a way to extend the state and behavior of any existing type via type embedding.