There is no current workaround method (or cheat code) to obtain the rail gun, other than the legitimate methods stated below.
Returning Player Requirement
You can obtain the Railgun in both GTA 5 story mode as well as GTA Online.
LATEST GTA NEWS CLIPS
However, to get it in story mode, you must be a returning GTA player. A returning GTA player is someone who played the PS3 or Xbox 360 version of the game and moved their account to the PS4 or Xbox One.
In GTA Online you can access the Railgun regardless of whether you are a returning player not. However, you can only access it in certain missions, since it is too powerful and would otherwise give you an unfair advantage over other players. In story mode you can use it whenever you want.
There are several locations where you can find the Railgun in GTA 5's story mode, which are as follows:
In Story Mode, if you are a returning player, you can easily buy the Railgun through any Ammu-Nation after completing the main story mission 'Minor Turbulence'. It will be available to purchase for $250,000.
Once you complete Minor Turbulence, head over to the crash site of the airplane from the mission at Alamo Sea. You will find a Railgun in the cockpit section of the wrecked plane. It is highly recommended that you first obtain a Scuba Diving Gear and then head over to the location, as the wreckage is deep underwater.
Shooting Range Method
If you haven't completed the mission yet, and accordingly the gun is not available to buy at the Ammu-Nation, then you can head over to the shooting range and select the "Bonus" challenge. This will let you choose a challenge that utilizes the Railgun. Then simply at your feet and shoot to kill yourself, which will spawn you back into the open world with the Railgun.
The Rail Gun can be used in a set number of Adversary Modes and Bunker Missions in GTA Online. Note that, in contrast to story mode, you can access the Railgun whether you are a returning player or not. In Adversary Modes, the railgun is part of different loadouts such as the Assault class setup in Beast vs. Slasher, Juggernaut's class in Power Mad, etc.
You will also find some of the Merryweather guards using it during Doomsday Heist missions.