Några highlights:
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
}
}
}()
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:
<?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;
}
}
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<p><?php echo $content ?></p>
</body>
</html>
<?php
class FirstpageModel {
static function getVars(){
return array(
'title' => 'Firstpage',
'content' => 'Firstpage content'
);
}
}
<?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.
This is a clichéish first post of a new blog.