10 myter om entreprenörskap

Några highlights:

  • Entreprenörskap är inget extraordinärt fenomen. Det kan hända vem som helst.
  • Man utgår inte ifrån mål, utan sina medel.
  • Man förlitar sig aldrig på en enskild lösning till ett problem.
  • Affärsplaner underlättar inte att förutse framtiden.
  • Entreprenörer försöker inte förutse framtiden, de formar den.

Swedish typography from the 50s and 60s

Tagged ,

How to capture SIGINT(ctrl-c) and SIGKILL in Go!

When exiting your Go! program it's important to finish any work in progress, this requires capturing the OS signals. One signal is called SIGINT(Interrupt) and is created by someone pressing Ctrl-C. This is the only one Go supports since it's consistant

From the Go package docs:

var (
    Interrupt Signal = syscall.SIGINT
    Kill      Signal = syscall.SIGKILL
)

The only signal values guaranteed to be present on all systems are Interrupt (send the process an interrupt) and Kill (force the process to exit).

import "os/signal"

signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
go func() {
    for signal := range signals {
        log.Printf("captured %v, exiting.", signal)
        // Tell app to exit
    }
}()

If we like to extend this concept we could also capture SIGKILL and multiplex the two channels:

sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
sigkill := make(chan os.Signal, 1)
signal.Notify(sigkill, os.Kill)
go func() {
    for {
        select {
            case <-sigint:
            log.Printf("captured SIGINT")
            // Tell app to exit gracefully
            case <-sigkill:
            log.Printf("captured SIGKILL")
            // Tell app to exit now
        }
    }
}()

os/signal package

Stackoverflow question

Tagged , ,

How to create your own MVC template-engine using PHP-files

How do you handle templates in PHP? There are many ways to do it. One way is to use PHP-files as templates. This approach enables independent loading (otherwise you always need to finish off the page with the view).

This is a simple yet powerful MVC-pattern for PHP:

template.php

<?php
// Load a php-file and use it as a template
function template($tpl_file, $vars=array()) {
    $dir='/usr/local/app/view/'.$tpl_file.'.php';
    if(file_exists($dir)){
        // Make variables from the array easily accessible in the view
        extract($vars);
        // Start collecting output in a buffer
        ob_start();
        require($dir);
        // Get the contents of the buffer
        $applied_template = ob_get_contents();
        // Flush the buffer
        ob_end_clean();
        return $applied_template;
    }
}

template_for_firstpage.php

<html>
    <head>
        <title><?php echo $title; ?></title>
    </head>
    <body>
        <p><?php echo $content ?></p>
    </body>
</html>

model_for_firstpage.php

<?php
class FirstpageModel {
    static function getVars(){
        return array(
            'title' => 'Firstpage',
            'content' => 'Firstpage content'
        );
    }
}

controller_for_firstpage.php

<?php
require "template.php";
require "model_for_firstpage.php";
$template_vars = FirstpageModel::getVars();
echo template('template_for_firstpage', $template_vars);

Nice and easily separated. Now you can pass the template-files to your designer and your model-files to your back-end engineers.

What does it do? * Template xml, xhtml, css, you name it * Separate Model, View, and Controller * Allow nesting of templates independently, first load footer, then header, then content, print it.

Tagged , ,

Mölndals kråka

First post

This is a clichéish first post of a new blog.