  Near maximum distance separable (NMDS) codes are important in finite geometry
and coding theory. Self-dual codes are closely related to combinatorics,
lattice theory, and have important application in cryptography. In this paper,
we construct a class of $q$-ary linear codes and prove that they are either MDS
or NMDS which depends on certain zero-sum condition. In the NMDS case, we
provide an effective approach to construct NMDS self-dual codes which largely
extend known parameters of such codes. In particular, we proved that for square
$q$, almost $q/8$ NMDS self-dual $q$-ary codes can be constructed.
