Lädt...


🔧 Three things that can go before a package clause in Go


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

This post is an excerpt from my email list, Boldy Go: Daily. Join for more content like this every day.

If you’ve done any Go coding at all, you’ve seen at least one package clause:

package main

And you probably know that it comes at the top of each Go source file.

But did you know there are three other things that may come first? Let's look at all three:

  • Comments, including GoDoc. It's good practice to include a descriptive GoDoc paragraph immediately before the package clause (i.e. with no blank lines between). You need only do this in a single file (in case of a multi-file package).

    // Package foo Foos all the Bars.
    package foo
    
  • Build constraints and other directives (these are treated as comments, but are worth mentioning separately).

    //go:build !js
    
    package foo
    

    In this example, we tell the compiler to ignore this file when building for a JS target (i.e. WebAssembly or GopherJS).

  • A shebang line (only as the very first line of the file).

    #!/usr/local/bin/go run
    package main
    

    What?!

    Didn't know that was possible?

    This actually isn't a Go thing at all, it's more a shell thing. But all common Unix/Linux shells will recognize such a line, and interpret it as the name of an interpretor to pass the file to. You really should not ever need this in Go, but some folks have fun writing Go for shell-script types of tasks. If that describes you, this will apply. I included this here mostly for completeness sake, not as an endorsement of this approach. 😉

What if we put all three together, just for fun?

#!/usr/local/bin/go run

// this comment does nothing special. It's not GoDoc, becuase
// there's no package clause or declaration on the next line,
// and it's not a build constraint.

//go:build !js

//go:generate /path/to/utility

// Package main is a utility that does something interesting.
package main
...

🔧 Three things that can go before a package clause in Go


📈 56.23 Punkte
🔧 Programmierung

🔧 Thất nghiệp tuổi 35


📈 38.33 Punkte
🔧 Programmierung

🔧 Things you should know before learning Three.js


📈 24.09 Punkte
🔧 Programmierung

📰 Three things to keep in mind before fully upgrading to Wi-Fi 6


📈 24.09 Punkte
📰 IT Security Nachrichten

📰 Three Things to Consider Before Incorporating Machine Learning into Your Security Efforts


📈 24.09 Punkte
📰 IT Security Nachrichten

📰 T-Mobile Says It Can't Be Sued By Users Because of Forced-Arbitration Clause


📈 23.43 Punkte
📰 IT Security Nachrichten

📰 Three things I love about Samsung's Galaxy Z Fold 4 (and two things I wish were better)


📈 23.39 Punkte
📰 IT Nachrichten

📰 From Cinema To Reality – Three Things Every Business Can Learn From Disaster Movies


📈 19.69 Punkte
📰 IT Security Nachrichten

📰 If you really can't let go of Windows 7, Microsoft will keep things secure for another three years


📈 19.69 Punkte
📰 IT Security Nachrichten

📰 From Cinema To Reality – Three Things Every Business Can Learn From Disaster Movies


📈 19.69 Punkte
📰 IT Security Nachrichten

🔧 Difference between WHERE vs HAVING Clause in SQL


📈 19.47 Punkte
🔧 Programmierung

📰 Santa Clause - Eine schöne Bescherung: TV-Sendetermine an Weihnachten 2019


📈 19.47 Punkte
📰 IT Nachrichten

📰 Disney+ bestätigt zweite Staffel von „Santa Clause: Die Serie“


📈 19.47 Punkte
📰 IT Nachrichten

📰 Cloud Server Host Vultr Rips User Data Ownership Clause From ToS After Web Outage


📈 19.47 Punkte
📰 IT Security Nachrichten

📰 Santa Clause - Eine schöne Bescherung: Weihnachtsfilm heute bei Sat.1 im TV


📈 19.47 Punkte
📰 IT Nachrichten

📰 Chase Bank Is Quietly Adding a Forced Arbitration Clause To Some Credit Cards


📈 19.47 Punkte
📰 IT Security Nachrichten

📰 Writing Window Functions with the Frame Clause


📈 19.47 Punkte
🔧 AI Nachrichten

🐧 SQL Having Clause


📈 19.47 Punkte
🐧 Linux Tipps

📰 "Santa Clause: Die Serie": Disney+ bringt Tim Allen als Weihnachtsmann zurück


📈 19.47 Punkte
📰 IT Nachrichten

🔧 Declaring Multiple Variables in a “for” Loop Initialization Clause


📈 19.47 Punkte
🔧 Programmierung

📰 Chase Bank Is Quietly Adding a Forced Arbitration Clause To Some Credit Cards


📈 19.47 Punkte
📰 IT Security Nachrichten

🐧 What if the De-facto FLOSS license was standard BSD with one extra clause?


📈 19.47 Punkte
🐧 Linux Tipps

🐧 MySQL INSERT IGNORE Clause


📈 19.47 Punkte
🐧 Linux Tipps

📰 Ärger um den Einsatz der Commons Clause


📈 19.47 Punkte
📰 IT Nachrichten

📰 DIB Orgs – Here’s How Your ISO 9001 Performance Evaluation Clause Helps with CMMC


📈 19.47 Punkte
📰 IT Security Nachrichten

📰 Do Not Put Filtering Conditions In the “WHERE” Clause in Some Scenarios


📈 19.47 Punkte
🔧 AI Nachrichten

🐧 The Commons Clause will destroy open source


📈 19.47 Punkte
🐧 Linux Tipps

📰 DIB Orgs – Here’s How Your ISO 9001 Improvement Clause Helps with CMMC


📈 19.47 Punkte
📰 IT Security Nachrichten

📰 Now Online Safety Act is law, UK has 'priorities' – but still won't explain 'spy clause'


📈 19.47 Punkte
📰 IT Security Nachrichten

🔧 Understanding OVER() Clause in SQL


📈 19.47 Punkte
🔧 Programmierung

matomo