Static assert at compile time

186 views
Skip to first unread message

hay

unread,
Oct 27, 2018, 6:07:08 PM10/27/18
to golang-nuts
Hi,

I've a project and it needs static asserts at compile time to check if string values are not left empty by the programmer. Is this possible in golang?

Thanks in advance

robert engels

unread,
Oct 27, 2018, 6:12:56 PM10/27/18
to hay, golang-nuts
I don’t think it is possible in any language - I think it is a very of the halting problem https://en.wikipedia.org/wiki/Halting_problem unless you are referring to checking a constant ?

--
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/d/optout.

Ian Denhardt

unread,
Oct 27, 2018, 6:26:33 PM10/27/18
to golang-nuts, hay
It's a little unclear to me what exactly you're trying to check; giving
an example might make it easier to advise.

But in any case, Go doesn't have anything quite like static_assert in
C/C++. Depending on what you're trying to do, you could put a call to
panic in an init() function, which would at least catch the error on
program start.

Quoting hay (2018-10-27 18:07:08)
> --
> 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 [1]golang-nuts...@googlegroups.com.
> For more options, visit [2]https://groups.google.com/d/optout.
>
> Verweise
>
> 1. mailto:golang-nuts...@googlegroups.com
> 2. https://groups.google.com/d/optout

hay

unread,
Oct 27, 2018, 7:12:01 PM10/27/18
to golang-nuts
Hi robert, I'm looking for something C/C++ like static assertion to check few things at compile time.

Hi Ian, thanks for the reply. It is checking at init at the moment.

It is basically string or/and version checks. I'll give simple example. In C++ it will be something like this.


class Foo
{
   
public:
       
static const string bar = "bar-version-01";
};

static_assert(Foo::bar != "
bar-version-01", "Foo::bar version not same :(");

Above code will not compile if Foo::bar is different.

David Collier-Brown

unread,
Oct 27, 2018, 7:42:08 PM10/27/18
to golang-nuts
In the special case of versioning, think about implementing "updaters" or "downdaters" on receipt of a versioned object/struct, as in https://leaflessca.wordpress.com/2017/02/12/dll-hell-and-avoiding-an-np-complete-problem/

For other uses, if you describe them here, we might be able to make a useful suggestion or two.

T L

unread,
Oct 27, 2018, 9:16:35 PM10/27/18
to golang-nuts
If the strings values are constants, you can use this trick to assert they are not blank at compile time.

For example, for a constant string with name Version, you can assure it is not blank by using

var _ = map[bool]int{false: 0, Version != "": 1}

Jan Mercl

unread,
Oct 28, 2018, 5:11:49 AM10/28/18
to T L, golang-nuts
On Sun, Oct 28, 2018 at 2:16 AM T L <tapi...@gmail.com> wrote:

> For example, for a constant string with name Version, you can assure it is not blank by using

> var _ = map[bool]int{false: 0, Version != "": 1}

Too complicated.

        const (
         Version = "v1.11"
        _          = 1 / len(Version)
        )

--

-j

Lucio

unread,
Oct 28, 2018, 9:04:55 AM10/28/18
to golang-nuts
Too clever! :-)

Lucio.

T L

unread,
Oct 28, 2018, 11:12:51 AM10/28/18
to golang-nuts
The var _ = map[bool]int{false: 0, contidion: 1} way is more general, it can be used to assert any conditions.

For OP's request, there are more ways, for example

type _ [len(Version)-1]int


But I admit the way shown by Jan Merci is the cleanest one. Cool!

T L

unread,
Oct 29, 2018, 12:46:20 AM10/29/18
to golang-nuts
Another two things ways:

var _ = Version[:1]
var _ = Version[0]
Reply all
Reply to author
Forward
0 new messages