os package and function

59 views
Skip to first unread message

sariu...@gmail.com

unread,
Jul 1, 2013, 12:10:02 PM7/1/13
to golan...@googlegroups.com
Hello! I wanna start by saying i`m new to programming and i started with GO. So i hope no one will flame or be mean. I`m trying to understand the following things. I have the program Hello World!

package
main
import "fmt"  
func main() { 
    fmt.Println("Hello World")
}

The next task sounds like this:
We used the Println function defined in the fmt package. If we wanted to use the Exit function from the os package what would we need to do?
I`m trying to understand the task. Since the fmt package contains the Println function then using the function displays the string content, the Exit function from os package would be used to exit the program right?
Can someone help me out with the code? How do I use the Exit function?

Dave Cheney

unread,
Jul 2, 2013, 12:59:58 AM7/2/13
to sariu...@gmail.com, golang-nuts
http://play.golang.org/p/bsskixt2sv
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Matt Silverlock

unread,
Jul 2, 2013, 1:14:31 AM7/2/13
to golan...@googlegroups.com, sariu...@gmail.com
Note that the program will exit after printing "Hello World" anyway, as there is nothing else for it to do, so the task is a bit contrived (and therefore confusing). What it's likely asking you to do is to:

import "os" 

and then call (after fmt.Println()):

os.Exit()

... in an effort to teach you the syntax of packages (fmt, os, log, net/http, etc.) and their methods (os.Exit(), log.Fatal() http.Handler(), etc.).

Given that you're new to programming, I'd highly suggest starting with a good book and working through it. Couple it with Google and Wikipedia to answer more "general" programming questions: 

http://www.golang-book.com/ (the best intro book, IMO; otherwise too basic if you have some programming chops)

There's also stuff like Coursera and OpenCourseWare that can help you out. OCW focuses on Python for many of the intro courses, but the principles are the same, and once you have a grasp of the high-level concepts you can translate them between languages. Python and Go aren't so dissimilar, which helps.
Reply all
Reply to author
Forward
0 new messages